在ios中取消NSURLConnection [英] cancel NSURLConnection in ios
问题描述
我在 mapView
drag / regionChange上调用webservice。我想在Web服务调用中保持一些延迟。因此,我希望每当用户多次拖动地图时,应取消之前的所有Web服务调用,并且只应触发上次拖动Web服务调用。
I am calling webservice on mapView
drag/regionChange. I want to keep some delay in the web service calls. So I want that whenever the user drags the map multiple times, all previous web service calls should be cancelled and only last drag web service call should be fired.
我如何这样做?
以下是我的代码:
{ .... NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[data length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];
NSMutableDictionary __block *dictResponse;
[[NSOperationQueue mainQueue] cancelAllOperations];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if(connectionError == nil){
dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&connectionError];
}
推荐答案
你无法取消 sendAsynchronousRequest
。如果您使用了基于委托的 NSURLConnection
请求,那么您可以取消它,但是(a)这比您可能想要打扰的编码更多; (b) NSURLConnection
已弃用,所以你应该 NSURLSession
;和(c) NSURLSession
允许您使用返回给您的 NSURLSessionTask
引用取消任务:
You cannot cancel a sendAsynchronousRequest
. If you used a delegate-based NSURLConnection
request, then you could cancel it, but (a) that's more coding than you probably want to bother with; (b) NSURLConnection
is deprecated, so you should be NSURLSession
anyway; and (c) NSURLSession
allows you to cancel the task using the NSURLSessionTask
reference that is returned to you:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"connection error = %@", error);
return;
}
NSError *parseError;
NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&parseError];
if (!responseObject) {
NSLog(@"parse error = %@", parseError);
}
dispatch_async(dispatch_get_main_queue(), ^{
// use response here; e.g., updating UI or model objects
});
}];
[task resume];
如果您需要取消此请求,只需致电 [任务取消]
。因此,如果您想跟踪它并稍后取消它,请将此 NSURLSessionTask
保存在某个弱变量中。
If you need to cancel this request, just call [task cancel]
. So save this NSURLSessionTask
in some weak variable if you want to keep track of it and cancel it later.
这篇关于在ios中取消NSURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!