NSURLSession background download lifecycle

I am using NSURLSession to do downloads in the background. I create the NSURLSession instance when my app launches in UIApplicationDelegate:application:didFinishLaunchingWithOptions:, always with the same session ID. This means that when the OS launches my app into the background to handle completed background transfers, the NSURLSession object will always be recreated before calling UIApplicationDelegate:application:handleEventsForBackgroundURLSession:completionHandler:. Does the implementation of the app lifecycle and NSURLSession guarantee that NSURLSessionDelegate:URLSessionDidFinishEventsForBackgroundURLSession: can never be called before UIApplicationDelegate:application:handleEventsForBackgroundURLSession:completionHandler: given that the session is always recreated before calling UIApplicationDelegate:application:handleEventsForBackgroundURLSession:completionHandler:?


Thank you.

Accepted Reply

I create the NSURLSession instance when my app launches in

-[UIApplicationDelegate application:didFinishLaunchingWithOptions:]
, always with the same session ID.

That’s the approach I recommend.

Does the implementation of the app lifecycle and NSURLSession guarantee that …?

Yes. The background session won’t start delivering events until

-application:handleEventsForBackgroundURLSession:completionHandler:
returns.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I create the NSURLSession instance when my app launches in

-[UIApplicationDelegate application:didFinishLaunchingWithOptions:]
, always with the same session ID.

That’s the approach I recommend.

Does the implementation of the app lifecycle and NSURLSession guarantee that …?

Yes. The background session won’t start delivering events until

-application:handleEventsForBackgroundURLSession:completionHandler:
returns.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Excellent, thank you for your response. It might be a good idea to note that in the documentation, as I could not find anything definitve either in the API docs or the guides. Rather, the docs seem to imply that the NSURLSession should be created and restored from application:handleEventsForBackgroundURLSession:completionHandler:, which is what made me uncertain.


By the way, regarding your notes on testing NSURLSession background code, I found that if I launch an app using Xcode on a physical device, initiate a download from the app, then click Xcode's stop button while the download is in progress, the background transfer process continues normally, and launches the app to handle the NSURLSession events, exercising the appropriate app delegate and session delegate API. I can view the app's logging as the background transfer completes using the Console app and filtering on my app's ID. I found this very useful to test and see the background transfer sequence execute. I would have added that comment to your notes, but the discussion is locked.


Thank you.


Robert

It might be a good idea to note that in the documentation, as I could not find anything definitve either in the API docs or the guides.

Good point. The best way to get this feedback to the folks who own the document is to file a bug against the relevant document. Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"