Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession with background session configuration is not returning an error when there is no connection

When I set up NSURLSession/Alamofire.Manager with a background session configuration, if there is no internet connection, I'm expecting to receive the usual NSError "Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline.".

This happened regularly if I'm not using a background configuration, but if I do such configuration my callback/delegate method will never be called. It will eventually be called when I activate the wifi again.

I'd prefer to receive an error straight away. Am I missing something?

like image 862
Luca Torella Avatar asked Oct 20 '15 16:10

Luca Torella


1 Answers

The reason that why the network failure in background session task does not return any error is:

In general an NSURLSession background session does not fail a task if something goes wrong on the wire. Rather, it continues looking for a good time to run the request and retries at that time. This continues until the resource timeout expires (that is, the value in the timeoutIntervalForResource property in the NSURLSessionConfiguration object you use to create the session). The current default for that value is one week!

I found the above answer at developer forum.

More details which might help in the background session:

Another benefit is that in a background session, we monitor the network and power environment for you. This means that we cover things like network reachability and connectivity for you, so you don't have to use the reachability APIs at all. We won't attempt to establish a connection until we know that the server is reachable. And similarly, if the user is performing a download and steps out of Wi-Fi, normally that task would then fail with a transmission error. But, in a background session, we'll actually recover from that automatically and retry it and resume where we left off if the download is resumable. And you won't hear about that error.

Source: WWDC 2014

like image 116
Utsav Dusad Avatar answered Oct 06 '22 20:10

Utsav Dusad