Reputation: 177
I am starting multiple AFJSONRequestOperation in a for loop like this:
for(NSString *obj in collection)
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//NSLog(@"success");
}
failure:nil];
[operation start];
How can I determine that all of my operations has finished?
I guess the operations are added a to the operationQueue
in AFHTTPClient but I am not sure. Eitherway I dont know how to access the instance of the AFHTTPClient being used.
Upvotes: 0
Views: 94
Reputation: 1846
Add the AFJSONRequestOperation
objects in a NSMutableArray
(I'm using operations
in this case) instead of starting each one, then use the method :
AFHTTPClient *client = [[AFHTTPClient alloc] initWithURL:baseURL];
[client enqueueBatchOfHTTPRequestOperations:operations progressBlock:nil completionBlock:^(NSArray *operations) {
// called when all operations have finished.
}];
Upvotes: 0
Reputation: 19098
Outside the loop, define a counter (int) which is initially set to the number of elements of collection. In each completion block (that explicitly means, in success AND failure handler!) decrement the counter by one. If it reaches zero, all requests have been finished.
Caution: I'm not completely sure, but as far as I know, AFJSONRequestOperation
won't invoke the handler blocks when it will be cancelled. Please consult the docs. (Personally, I think this is a design issue - but alas ... )
There are more elegant solutions to this problem. However, this would require a more specialized set of classes and tools which help to implement common asynchronous design patterns, like this one.
Upvotes: 0