AFHTTPRequestOperation取消请求

时间:2013-12-17 15:49:29

标签: 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];

所以我把“操作”放到“operationQueue”并且可以用以下命令停止操作:

[operationQueue cancelAllOperations];

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

  

错误域= NSURLErrorDomain代码= -999“无法完成操作。

2 个答案:

答案 0 :(得分:12)

您可以使用:

[operation cancel];

这是一种由AFNetworking实施的NSOperation方法。

答案 1 :(得分:0)

您可以使用此功能取消所有操作:

[[httpClient operationQueue] cancelAllOperations];
相关问题