当我使用URLSession进行POST API调用时,在API执行过程中发生网络断开,我收到来自URLSession的网络错误回调-
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=53, NSUnderlyingError=0x2822cc9f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=53, _kCFStreamErrorDomainKey=1}}
然后,如果网络立即重新连接,URLSession将静默重试API调用,并且我不会收到对所做API调用的任何确认.
当我们收到网络错误时,我们认为调用失败,并try 重试API.至此,进行了2次API调用,one from my retry mechanism and another from URL session auto retry mechanism.次
有什么方法可以告诉URL会话不要自动重试吗?对于我来说,重要的是避免对后端进行重复的API调用.
这是我的URLSession配置-
let config = URLSessionConfiguration.default
config.waitsForConnectivity = true
config.timeoutIntervalForRequest = 1
config.timeoutIntervalForResource = 1
URLSession.shared.dataTask(with: request.urlRequest!) {data, response, error in
// Do something
}.resume()