swift – Disable URLSession auto retry mechanism in iOS


When i make post API call using URLSession and a network disconnection happens while the API is in progress, i receive a network error callback from 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}}

Then if the network is immediately reconnected, the URLSession silently retries the API call and i don’t receive any acknowledgement of the API call made.

As we received network error, we believe that the call has failed and attempt to retry the API. At this point 2 API calls were made, one from my retry mechanism and another from URL session auto retry mechanism.

Is there any way i can tell URL session not to auto retry? It is important for me to avoid making duplicate API calls to the backend.

This is my URLSession configuration –

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()

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img