Ios AFHTTPClient+;EnqueueBatchOftPrequesStoperations:处理取消和错误
我的应用程序需要从web上获取一些图像,但我希望用户能够取消此下载(如果没有连接,或者速度太慢,或者w/e)。应用程序界面在任何情况下都不应该被“冻结”。因此,我使用Ios AFHTTPClient+;EnqueueBatchOftPrequesStoperations:处理取消和错误,ios,multithreading,afnetworking,afhttpclient,Ios,Multithreading,Afnetworking,Afhttpclient,我的应用程序需要从web上获取一些图像,但我希望用户能够取消此下载(如果没有连接,或者速度太慢,或者w/e)。应用程序界面在任何情况下都不应该被“冻结”。因此,我使用AFHTTPClient和EnqueueBatchOfHttPrequesToOperations:progressBlock:completionBlock:方法下载: NSMutableArray *operationsArray = [NSMutableArray array]; for (NSString *imageURL
AFHTTPClient
和EnqueueBatchOfHttPrequesToOperations: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:
,因为它保证所有操作都已完成,并且我获得了NSArray*操作的访问权限,所以我可以检查它是错误还是取消,就像我在失败:
中所做的那样。但我发现,在这种情况下,这个块甚至没有执行(可能是因为isCancelled、isFinished、isExecuting属性的机制)
那个么,若出现错误或者用户按下了“取消下载”按钮,我应该如何删除UI阻塞对话框并取消下载呢
更新
我不知道为什么,但在本例中,取消检查在completionBlock:
中,正是我要放它的地方!。但是在我的例子中,如果任何操作被取消,这个块就不会执行!也许我在配置我的AFHTTPClient时遗漏了什么?以取消所有操作使用
[[client operationQueue] cancelAllOperations];
要删除UI阻止对话
您必须包含删除拨号的代码
你把这个代码叫做什么
在完成区块时成功完成
说清楚
- (void)cancelDownloadDialogButtonClicked {
[afhttpClient.operationQueue cancelAllOperations];
//DO Remove the UI blocking mechanism here Eg.
[SVProgressHUD dismiss];
}
是的,我喜欢。我的第一篇文章有点失败,复制了cancelDownloadDialogButtonClicked
,但没有调整到这个示例代码。问题是,下一步该怎么做?在何处捕获此取消已完成,并删除阻塞视图?可能我应该在imageRequestOperationWithRequest:
的failure:
块中添加一些计数器来完成。或者就在[afhttpClient.operationQueue cancelAllOperations]之后代码>与[afhttpClient.operationQueue waituntlalloperations完成]代码>?