有没有办法取消AFHTTPRequestOperation?例如,我正在使用它来下载PLIST。所以我打算有一个按钮来取消下载或任务。 有没有办法?AFHTTPRequestOperation取消请求
更新:
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”,可以与停止操作:
[operationQueue cancelAllOperations];
这是在队列中只需一人操作。如果有不止一个,有没有办法明确停止操作?我猜在控制台中出现以下错误 - 如果队列被取消,这是否正常?
错误域= NSURLErrorDomain代码= -999“的操作无法完成
如何从另一个(void)中调用此函数,因为只有在调用连接的函数中才知道操作。 – halloway4b
你有几个选择。例如,你可以将'operation'保存为弱@属性,或者你可以将它添加到NSOperationQueue而不是调用'start',然后从同一个队列中找到它。下面是NSOperation的一个很好的概述:http://nshipster.com/nsoperation/ –
感谢NSOperation的提示。上面更新了我的代码。我认为如果操作被取消,控制台日志中的错误是正常的? – halloway4b