Cancelling batch request in AFNetworking

2019-04-16 02:55发布

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条回答
乱世女痞
2楼-- · 2019-04-16 03:52

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
        });
      }
    }
查看更多
登录 后发表回答