gpt4 book ai didi

ios - AFHTTPClient + enqueueBatchOfHTTPRequestOperations : handle cancellation and errors

转载 作者:行者123 更新时间:2023-11-29 03:52:49 30 4
gpt4 key购买 nike

我的应用程序需要从网络获取一些图像,但我希望用户能够取消此下载(如果没有连接,或者速度太慢,或者没有)。在任何情况下,应用程序界面都不应该被“卡住”。因此,我使用 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 阻止对话框)。在我看来,它的最佳位置是在 enqueueBatchOfHTTPRequestOperations:completionBlock: 中,因为它保证所有操作都已完成,并且我可以访问 NSArray *操作,这样我就可以检查它是错误还是取消,就像我在failure:中所做的那样。但我发现在这种情况下这个 block 甚至没有执行(可能是因为 isCancelled、isFinished、isExecuting 属性机制)。

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

更新

不知道为什么,但在这个例子中Cancelling batch request in AFNetworking取消检查位于 completionBlock: 中,正是我要放置的位置!但就我而言,如果取消任何操作,则该 block 不会执行!也许我在配置 AFHTTPClient 时遗漏了一些东西?

最佳答案

取消所有操作使用

[[client operationQueue] cancelAllOperations];

删除 UI 阻塞对话框

您必须包含用于删除该对话框的代码

  1. 在哪里调用此代码
  2. 成功完成区 block 时

说清楚

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

关于ios - AFHTTPClient + enqueueBatchOfHTTPRequestOperations : handle cancellation and errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16894610/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com