AFHTTPRequestOperation Cancel Request

泪湿孤枕 提交于 2020-01-11 09:18:34

问题


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?

Update:

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];

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?

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


回答1:


You can use:

[operation cancel];

This is an NSOperation method that's implemented by AFNetworking.




回答2:


You could use this to cancel all operations:

[[httpClient operationQueue] cancelAllOperations];


来源:https://stackoverflow.com/questions/20638611/afhttprequestoperation-cancel-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!