Reputation: 1387
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.
Upvotes: 1
Views: 1468
Reputation: 10407
No, There's no way to start multiple tasks at once.
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.
Upvotes: 1