3
votes

I have a prototype single-view app which monitors download tasks.

enter image description here

I'm trying to deal with following use case:

  1. Downloads are initiated via NSURLSession while app is in foreground. NSURLSession is created with background configuration.
  2. I kill the app with "Xcode Stop", so that app continues download in background. While app is alive, I orderly receive progress callbacks from NSURLSession.
  3. I manually start the app (not by Xcode, but tapping the launch icon), when the downloads have not been completed yet.
  4. I don't receive any URLSession delegate calls for tasks started in previous app's life. The only thing that gets called is handleEventsForBackgroundURLSession but that's called on AppDelegate by the OS (different case than NSURLSession delegate calls).

I want to show progress of ongoing download tasks. Can this be done after app relaunch (when app was terminated by the system, not manually!)?

After app relaunch, NSURLSession is initialized with same identifier, new delegate object, so I figured delegate will continue to receive calls for session's tasks (because session identifier is the same), but apparentely that's not the case. There is a note in Apple's documentation:

The session object keeps a strong reference to the delegate until your app explicitly invalidates the session. If you do not invalidate the session, your app leaks memory.

but I guess this only applies to case when app is alive. When the app is terminated, then all app's objects are gone.

2
hey , in this BG Transfer Demo , u achieved to resume downloading after app remove from backhround ???Monika Patel
I don't resume download if the app was manually (by the user) removed from the background. When that happens, iOS probably cancels URL sessions. Once app is restarted, you can manually check the state of your persisted tasks and do proper actions. If you like, I can send you the "BG Transfer Demo" or upload to some repository for you to checkout.mixtly87
Yes, Please send me demo on my gmail id. [email protected] & [email protected] Thanks in advance.Monika Patel

2 Answers

1
votes

Make sure NSURLSession is properly initialised when app launches. That's what the problem was in my case. I had TransferManager which initialised session as lazy getter which was not getting invoked...

Now that the NSURLSession is properly initialised, callbacks are fired regularly.

Stupid error, but there it is.

0
votes

You goal seems to be in number 4, trying to receive URLSession delegate callbacks while in the background. I've been struggling with that myself and wasn't able to find a great solution, however I did realize that whenever I performed any actions (even simply calling the completionHandler() callback) in handleEventsForBackgroundURLSession: I received a call to

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error {

Seems like performing operations might wake up the delegate that was specified when creating your NSURLSession.

That is the correct method to perform any UI updates (such as showing progress) since that's the only location you'll know the background task is complete. Just make sure to call the completion handler callback after you are done!

Also this may be of some help to you: My NSURLSessionDelegate methods are not getting called during a background download