我enqueueBatchOfHTTPRequestOperations
用来提交一批请求。如果任何请求失败,我想立即取消任何其他仍在进行的请求。为此,我将单个操作的失败回调设置为执行[client.operationQueue cancelAllOperations];
.
这似乎取消了所有剩余的操作,但它也阻止了批处理的整体完成块执行......这是我试图测试这种行为的代码(其中一个请求总是在服务器上设置为失败)。
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://arahlf.com"]];
NSMutableArray *requests = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"echo.php" parameters:@{ @"sleep": @(i) }];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:nil failure:nil];
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request failed, cancelling all operations.");
[client.operationQueue cancelAllOperations];
}];
[requests addObject:operation];
}
[client enqueueBatchOfHTTPRequestOperations:requests progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"Progress: %i/%i", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
NSLog(@"All done!");
}];
对我来说,completionBlock 永远不会被执行。此外,由于一个失败的请求取消了剩余的请求(这也触发了失败块),cancelAllOperations
实际上被执行了很多次。
有没有更好的方法来达到这个效果?