Cancelling batch request in AFNetworking

偶尔善良 提交于 2019-12-06 13:16:54

问题


So I have a batch request which is the following code:

[[AHClient sharedClient] enqueueBatchOfHTTPRequestOperationsWithRequests:requestArray progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {


    } completionBlock:^(NSArray * operations){

        dispatch_async(dispatch_get_main_queue(), ^(void){
           //update the UI
        });
    }];

I tried cancelling the request by saving the path of the url in an array and do the following:

for (NSString * urlPath in self.currentRequestArray_){
        [[AHClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:urlPath];
    }

but it seems that it still goes to the completed block, i.e: updates the UI. Thoughts or suggestions?


回答1:


In the batch completion block, check that the component operations aren't cancelled, and only perform the action if any of them finished successfully.

    completionBlock:^(NSArray * operations){
      if ([[operations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isCancelled == NO"]] count] > 0) {
        dispatch_async(dispatch_get_main_queue(), ^(void){
           //update the UI
        });
      }
    }


来源:https://stackoverflow.com/questions/10592289/cancelling-batch-request-in-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!