I have been using NSURLConnection's
sendAsynchronousRequest:queue:completionHandler:
method which is great. But, I now need to make multiple requests in a row.
How can I do this while still using this great asychronous method?
There's lots of ways you can do this depending on the behavior you want.
You can send a bunch of asynchronous requests at once, track the number of requests that have been completed, and do something once they're all done:
NSInteger outstandingRequests = [requestsArray count];
for (NSURLRequest *request in requestsArray) {
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[self doSomethingWithData:data];
outstandingRequests--;
if (outstandingRequests == 0) {
[self doSomethingElse];
}
}];
}
You could chain the blocks together:
NSMutableArray *dataArray = [NSMutableArray array];
__block (^handler)(NSURLResponse *response, NSData *data, NSError *error);
NSInteger currentRequestIndex = 0;
handler = ^{
[dataArray addObject:data];
currentRequestIndex++;
if (currentRequestIndex < [requestsArray count]) {
[NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:currentRequestIndex]
queue:[NSOperationQueue mainQueue]
completionHandler:handler];
} else {
[self doSomethingElse];
}
};
[NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:0]
queue:[NSOperationQueue mainQueue]
completionHandler:handler];
Or you could do all the requests synchronously in an ansynchronous block:
dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("Lots of requests", NULL);
dispatch_async(downloadQueue, ^{
for (NSRURLRequest *request in requestsArray) {
[dataArray addObject:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]];
}
dispatch_async(callerQueue, ^{
[self doSomethingWithDataArray:dataArray];
});
});
});
P.S. If you use any of these you should add some error checking.
dispatch_async
here is using a background thread. But there's nothing wrong with calling synchronous requests from a background request. In fact, the documentation explicitly discourages you from making synchronous requests in the main thread of a GUI app since it'll block the UI.
sendAsynchronousRequest:queue:completionHandler:
sends a request asynchronously (i.e., in the background, not blocking the main thread). When the response comes back, the handler block is dispatched to the queue specified in the queue specified.
completionHandler
s in this example are not being executed in parallel threads. They get added to the mainQueue
and executed one at a time. There's presumably some kind of multithreading going on behind the scenes in Cocoa's internals to handle putting the blocks onto the mainQueue
as the HTTP responses arrive, but it's abstracted away and you don't need to worry about it. If you've worked with AJAX in JavaScript before, this behaviour with sequential execution of completion handlers will be familiar to you.
Aug 27, 2013 at 14:27