AFNetworking:enqueueBatchOfHTTPRequestOperations问题与完成块

Fel*_*lix 12 iphone nsoperation nsoperationqueue ios afnetworking

我使用这种AFNetworking方法一次启动多个请求:

- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations 
                              progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock 
                            completionBlock:(void (^)(NSArray *operations))completionBlock
Run Code Online (Sandbox Code Playgroud)

其中一个是AFJSONRequestOperation.问题是此JSON操作的成功块是在批处理的完成块之后执行的.原因是:AFJSONRequestOperation有一个用于JSON处理的内部调度队列.因此,在调用完成块时,JSON数据仍在处理中.

问题:如何在调用JSON操作的成功块之后在完成块中执行代码?

我试图在主队列上调度代码块,但这没有帮助.

Jam*_*hen 1

如果可能的话,最简单的解决方案可能是将处理代码从每个操作的成功块移动到整个批次的完成块。

NSArray *operations在完成块中有可用的内容,您可以迭代操作并查找:

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

operation.request.URL如果您需要执行不同的操作,您还可以通过该属性获得每个操作的 url 地址