When does NSURLSession run?
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's Data Task automatically resume in the background, if internet connection is lost?
NSURLSession automatically attempt to complete the task if there is no internet initially, or does the session just return with an error?
- Swift3 iOS -Firebase FIREmailPasswordAuthProvider Not Working
- How to pop a viewController and then Push a viewController through delegation
- Photo capture permission problems in iOS 11
- MailComposer didFinishWith results doesn't work in Swift 3.0
- How to sort files by modified date in iOS
- How to check if an NSString contains one of the NSStrings in an NSArray?
3 Solutions Collect From Internet About “When does NSURLSession run?”
Looks like it’s down to you to handle the retry.
When any task completes, the NSURLSession object calls the delegate’s
URLSession:task:didCompleteWithError: method with either an error
object, or nil if the task completed successfully. If the task is a
resumable download task, the NSError object’s userInfo dictionary
contains a value for the NSURLSessionDownloadTaskResumeData key. Your
app should use reachability APIs to determine when to retry, and
should then call downloadTaskWithResumeData: or
downloadTaskWithResumeData:completionHandler: to create a new download
task to continue that download. Go to step 3 (creating and resuming
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 method
URLSession: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 method
URLSession: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).
URL Loading System Programming Guide
Foundation Constants Reference
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”
- LibXML2.dylib and Xcode4
- Failed build b/c symbols not found zlib
- Facebook iOS8 SDK build module error for FBSDKCoreKit
- Triggering a specific action when the app enters foreground from a local notification in iOS? (using swift)
- Swift JSON error : Could not cast value of type '__NSDictionaryM' to 'NSArray'
- iAd alternative for ios ad display?
- How to open iOS app from browser?
- Upload Multiple Images to Server IOS
- How to toggle running Xcode project in debug or release mode by using only shortcut keys?
- getting pcm audio for visualization via Spotify iOS SDK
- Unable to use self signed certificate with AFNetworking 2
- Draw a path with variable width in iOS
- How to keep project available after changing the location of imported project?
- WKWebView equivalent for UIWebView's scalesPageToFit
- RFQuiltLayout and UICollectionView