7

Here is the question: how to download a number of files one by one using the new Background Transfer Service (including the case when the app is suspended)? I read this awesome tutorial on objc.io and got it working for one file. But I need to download files one by one (so adding multiple NSURLSessionDownloadTaskss will not work (since download URLs are only valid for a short amount of time)

Basically what I am trying to do is to schedule another download once the app is notified that the previous download finished in application:handleEventsForBackgroundURLSession:completionHandler:. But I only get this method invoked once. Any idea why? Any advice on how to implement sequential downloads for multiple files when the app is suspended is appreciated.

UPDATE:

Sorry, I probably was not clear about what the actual problem is: it's not that I do not get notified about the task completion in general, it's that I do not have application:handleEventsForBackgroundURLSession:completionHandler: called for the second download task when the app is running in the backgorund. I do get it invoked for the first download task (which started while the app was in the foreground and then went to background before the download finished) then I fire the second download task, call the completionHandler I got in application:handleEventsForBackgroundURLSession:completionHandler: and never have this method invoked for the second file.

dariaa
  • 6,285
  • 4
  • 42
  • 58

2 Answers2

2

I would suggest adding next file in NSURLSessionTaskDelegate's - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error. This method is called whenever previous task completes, so looks like a reasonable choice to enqueue next file.

Andrey
  • 1,561
  • 9
  • 12
  • Please see the updated question. Firing the second download in `URLSession:task:didCompleteWithError:` does not solve the problem, I still do not have `application:handleEventsForBackgroundURLSession:completionHandler:` called for the second file. – dariaa Apr 04 '14 at 11:24
  • Hmm, I see. Definitely seeing strange behaviour: I think I've reproduce what you're describing in a sample PoC app. What is puzzling, however, is that larger app we have in prod doesn't exhibit similar behaviour (and my answer was based on that). Not quite sure what's happening there :/ – Andrey Apr 04 '14 at 20:46
  • 3
    The [excellent article](http://www.objc.io/issue-5/multitasking.html) linked by the O.P., mentions that: if your applications initiates a background transfer _while it is in the background_, that background transfer will always get the `discretionary` flag. With the approach above, the second, and any future transfers, will be initiated while the application is in the background. This means they'll only complete when the device has WiFi (not cellular) and good battery. I think they'll also be batched. Perhaps it _is_ working, but it's just taking a while? – Benjohn Aug 12 '14 at 04:57
1

From a perspective of this tutorial here (http://www.appcoda.com/background-transfer-service-ios7/) it looks like you have to start the download of those two files simultaneously. As you have a configuration for the maximum connections per host in a session, I guess you can limit the parallel downloads to 1, and just kick off both downloads.

I am currently trying to port this to MonoTouch ... seems promising ...

SimonSimCity
  • 6,415
  • 3
  • 39
  • 52
  • what if we face a situation that we should download multiple files consecutively in background when task1 is complete task2 is begin and if task1 fails app terminates? – Hadi zamani Dec 05 '15 at 12:18
  • @Hadizamani As iOS is first downloading the file to a temporary folder, you'll not see anything of the partially downloaded file. I guess you'll neither be able to get access to the already downloaded part of the file. – SimonSimCity Dec 07 '15 at 07:42