I have a situation in which when the device receives a push notification, I would send a NSURLSession
combined with a NSURLConnection
. I have a couple of questions regarding NSURLSession
.
Does NSURLSession's
Data Task automatically resume in the background, if internet connection is lost?
Does NSURLSession
automatically attempt to complete the task if there is no internet initially, or does the session just return with an error?
If you use background configurations with NSURLSession the delegates are getting called on success/failure when your app is Active or in the background. The task you created are handed over to a demon of the OS and your app is being waken up or even started (if it killed by iOS) using
And when all the tasks completed your session delegate would be called with
More details on background session are here
You can also get apple sample code here and look for "Simple Background Transfer"
Does NSURLSession's Data Task automatically resume in the background, if internet connection is lost?
If the internet connection is lost you will receive an error,
NSURLErrorNetworkConnectionLost
(error code: -1005), in the delegate methodURLSession:task:didCompleteWithError
. You are responsible for retrying and/or invalidating the session using invalidateAndCancel or finishAndInvalidate (it will have already finished though in the case of a network connect loss).Does NSURLSession automatically attempt to complete the task if there is no internet initially, or does the session just return with an error?
If there is no internet connection you will receive an error,
NSURLErrorNotConnectedToInternet
(error code: -1009), in the delegate methodURLSession:task:didCompleteWithError
. You are responsible for invalidating the session (if needed) using invalidateAndCancel or finishAndInvalidate (it will have already finished though in the case of no internet connection).References:
URL Loading System Programming Guide
Foundation Constants Reference
Looks like it's down to you to handle the retry.
https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html