[英]cancel NSURLConnection in ios
I am calling webservice on mapView
drag/regionChange. 我在mapView
drag / regionChange上调用webservice。 I want to keep some delay in the web service calls. 我想在Web服务调用中保持一些延迟。 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. 因此,我希望每当用户多次拖动地图时,应取消所有先前的Web服务调用,并且只应触发最后一次拖动Web服务调用。
How do I do this? 我该怎么做呢?
Following is my code: 以下是我的代码:
{ .... 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];
}
You cannot cancel a sendAsynchronousRequest
. 您无法取消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; 如果您使用了基于委托的NSURLConnection
请求,那么您可以取消它,但是(a)这比您可能想要打扰的编码更多; (b) NSURLConnection
is deprecated, so you should be NSURLSession
anyway; (b) NSURLConnection
已弃用,因此无论如何你应该是NSURLSession
; and (c) NSURLSession
allows you to cancel the task using the NSURLSessionTask
reference that is returned to you: (c) NSURLSession
允许您使用返回给您的NSURLSessionTask
引用取消任务:
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];
If you need to cancel this request, just call [task cancel]
. 如果您需要取消此请求,只需调用[task cancel]
。 So save this NSURLSessionTask
in some weak variable if you want to keep track of it and cancel it later. 因此,如果要跟踪它并稍后取消它,请将此NSURLSessionTask
保存在某个弱变量中。
Don't use the sendAsynchronousRequest
convenience API, use the main delegate API so you have access to the instance of NSURLConnecyion
to call cancel
on. 不要使用sendAsynchronousRequest
便捷API,使用主委托API,这样您就可以访问NSURLConnecyion
实例来调用cancel
。
Also, don't cancel operations on the main queue, you don't know what's there, and consider using NSURLSession
另外,不要取消主队列上的操作,你不知道那里有什么,并考虑使用NSURLSession
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.