AFHTTPRequestOperation取消请求 [英] AFHTTPRequestOperation Cancel Request
问题描述
有没有办法取消AFHTTPRequestOperation?我用它来下载PLIST例如。所以我打算用一个按钮来取消下载或任务。
有办法吗?
Is there a way to cancel a AFHTTPRequestOperation? I am using it to download a PLIST for example. So I was planning to have a button to cancel the download or task. Is there a way?
更新:
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];
所以我把操作放到operationQueue并且可以用以下命令停止操作:
So I have put "operation" to "operationQueue" and can stop the operation with:
[operationQueue cancelAllOperations];
这只是队列中的一个操作。如果有多个,是否有办法显式停止操作?我猜控制台中存在以下错误 - 如果队列被取消,这是正常的吗?
This is only one operation in the queue. Is there a way to stop operation explicit if there are more than one? I guess the following error in the console - is it normal if a queue gets cancelled?
错误域= NSURLErrorDomain代码= -999操作无法完成。
Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed.
推荐答案
您可以使用:
[operation cancel];
这是一种由AFNetworking实施的NSOperation方法。
This is an NSOperation method that's implemented by AFNetworking.
这篇关于AFHTTPRequestOperation取消请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!