1

I would like to use NSURLSession and dataTaskWithURL:completionHandler: to download 4 different URLs into NSData objects. I know I can call:

[session dataTaskWithURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {
    // handler
}];

Once these are added to the session I know I can start them with [task resume]. Is there any way to tell the session to simply start all the tasks I have added to it?

But how can I get notified when all four of them are finished (with an error or otherwise)? Do I need to keep some sort of thread-safe dictionary around to keep track of the state of each task?

My purpose is that once these are downloaded, I need to start a timer so the same four URLs can be downloaded again sometime in the future.

spenibus
  • 4,339
  • 11
  • 26
  • 35
Trygve
  • 1,317
  • 10
  • 27

1 Answers1

1
  1. No, There's no way to start multiple tasks at once.

  2. Yes, you need to keep a dictionary to store the responses, using the data tasks as a key. Perform all modifications and accesses on the main thread for safety.

You might also consider a dispatch group, as described in this question.

Community
  • 1
  • 1
dgatwood
  • 10,129
  • 1
  • 28
  • 49