1

I have multiple web services to be checked and downloaded in the background of my app, using NSURLSession to do this. When the app is in the background I use this code:

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    [self methodToStartAllDownloads];
    //Completion handler called somehow
}

The problem is that since all NSURLSessions have a completion handler and working async, it's hard to know when all of them are finished, so that I can call completionHandler(UIBackgroundFetchResultNewData) in the method above. Is there a way to achieve something like calling the completion handler when all downloads are complete?

Jorn
  • 1,054
  • 9
  • 25
  • Any updates on this? – Marcel Gruber Nov 11 '15 at 00:04
  • 1
    I used a global variable to count the number of completed downloads and call the completion handler when all downloads were done. However, this is not a very good solution and I'm still looking for a better solution. – Jorn Nov 12 '15 at 16:21

1 Answers1

1

You can call the completion handler after [self methodToStartAllDownloads];, when you use NSURLSession with a background configuration, your downloads and uploads are handed to the system who will wake your app up once all of your tasks are completed.

I suggest you read this article to understand the NSURLSession background better

Leonardo
  • 1,740
  • 18
  • 27