ios - AFHTTPRequestOperation 取消请求

标签 ios afnetworking-2

有没有办法取消 AFHTTPRequestOperation?例如,我用它来下载 PLIST。所以我打算有一个按钮来取消下载或任务。 有办法吗?

更新:

operationQueue = [NSOperationQueue new];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFPropertyListResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id propertyList) {

    NSLog(@"DONE");        

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"%@", error);

}
 ];

 [operationQueue addOperation:operation];

所以我已经将“operation”放入“operationQueue”并且可以通过以下方式停止操作:

[operationQueue cancelAllOperations];

这只是队列中的一个操作。如果有多个,有没有办法停止显式操作?我猜控制台中出现以下错误 - 如果队列被取消是否正常?

Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed.

最佳答案

您可以使用:

[operation cancel];

这是一个由 AFNetworking 实现的 NSOperation 方法。

关于ios - AFHTTPRequestOperation 取消请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20638611/

相关文章:

ios - UILocalNotification 中的警报未被调用

ios - 在 iOS 中使用 AFNetworking 2 检查连接的最佳方法?

ios - 如何使用 Storyboard 重新排列 UITabBarController 项目?

ios - XcodeBuild 需要 Target 的配置文件吗?错误域=IDEProvisioningErrorDomain 代码=9

android - multer, react 原生图像选择器图像上传在 iOS 上不起作用

iOS 适用于通过 http 处理双向同步的最高级别网络抽象是什么?

ios - 在发出任何请求之前验证服务器身份

ios - 使用 AFNetworking/AFHTTPRequestOperation 处理意外的响应内容类型

ios - AFNetworking 2.0 - 可变 json

java - 如何将在 iOS 或 Android 上运行的 JavaFX 应用程序连接到 native 库?