AFHTTPClient + enqueueBatchOfHTTPRequestOperations:处理取消和错误

时间:2013-06-03 10:23:47

标签: ios multithreading afnetworking afhttpclient

我的应用程序需要从网络上获取一些图像,但我希望用户能够取消此下载(如果它没有连接,或者它太慢,或者没有)。在任何情况下,App界面都不应该“冻结”。所以我正在使用AFHTTPClientenqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:方法进行下载:

NSMutableArray *operationsArray = [NSMutableArray array];
for (NSString *imageURL in imageURLArray) {
    AFImageRequestOperation *getImageOperation =
    [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]]
                                         imageProcessingBlock:nil
                                                      success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                                          //
                                                          // Save image
                                                          //
                                                      }
                                                      failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                                          if((error.domain == NSURLErrorDomain) && (error.code == NSURLErrorCancelled))
                                                              NSLog(@"Image request cancelled!");
                                                          else
                                                              NSLog(@"Image request error!");
                                                      }];
    [operationsArray addObject:profileImageOperation];
}
//
// Lock user interface by pop-up dialog with process indicator and "Cancel download" button
//
[afhttpClient enqueueBatchOfHTTPRequestOperations:operationsArray
                              progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
                                  //
                                  // Handle process indicator
                                  //
                              } completionBlock:^(NSArray *operations) {
                                  //
                                  // Remove blocking dialog, do next tasks
                                  //
                              }];

如果按下“取消下载”按钮:

- (void)cancelDownloadDialogButtonClicked {
    [afhttpClient.operationQueue cancelAllOperations];
}

我的问题是:

我不知道应该在哪里检查操作错误和取消(我希望在这种情况下取​​消整个下载并删除UI阻止对话框)。 在我看来,它的最佳位置是completionBlock: enqueueBatchOfHTTPRequestOperations:,因为它保证所有操作都已完成,并且我可以访问NSArray *operations,所以我可以检查是否错误或取消,就像我在failure:中所做的那样。但是我发现这个块在这种情况下甚至没有执行(可能是因为isCancelled,isFinished,isExecuting propertys mechanics)。

那么如果出现错误或用户按下“取消下载”按钮,我应该如何删除UI阻止对话框并取消下载?

更新

不知道为什么,但在这个示例中Cancelling batch request in AFNetworking取消检查在completionBlock:,正好在我要放的地方!但在我的情况下,如果任何操作被取消,则该块不会执行!也许我在配置我的AFHTTPClient时遗漏了一些东西?

1 个答案:

答案 0 :(得分:1)

取消所有操作使用

[[client operationQueue] cancelAllOperations];

删除UI阻止对话

您必须在

中包含删除该拨号的代码
  1. 您在此处调用此代码
  2. 在完成阻止成功完成后
  3. 要说清楚

    - (void)cancelDownloadDialogButtonClicked {
        [afhttpClient.operationQueue cancelAllOperations];
        //DO Remove the UI blocking mechanism here Eg.
        [SVProgressHUD dismiss];
    }