Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking: enqueueBatchOfHTTPRequestOperations issue with completion block

I use this AFNetworking method to start multiple requests at once:

- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations 
                              progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock 
                            completionBlock:(void (^)(NSArray *operations))completionBlock

One of them is a AFJSONRequestOperation. The problem is that the success-block of this JSON operation is executed after the completion-block of the batch. The reason is: AFJSONRequestOperation has an internal dispatch queue for JSON processing. So the JSON data is still in processing while the completion block is called.

Question: How can execute code in the completion block after the success block of the JSON operation has been called?

I tried to dispatch a code block on the main queue but that didn't help.

like image 281
Felix Avatar asked Jun 15 '12 12:06

Felix


1 Answers

If it's possible, the simplest solution might be just to move your processing code from the success block of each operation to the completion block of the whole batch.

You have the NSArray *operations available in the completion block, you can iterate through the operations and look for:

for(AFHTTPRequestOperation *operation in operations){
   if(operation.response.statusCode == 200){
      //Do something with the response
   }else{
     //Handle the failure
   }
}

You also have the url address for each operation available through the operation.request.URL property if you need to preform different actions

like image 165
James Cowhen Avatar answered Oct 20 '22 04:10

James Cowhen