Yin*_*ing 1 nsurlconnection nsurlrequest ios
我知道有关ios上的nsurlconnection的讨论,并且超时至少有240秒.我的问题是,如果我通过NSURLConnection发送同步调用+(NSData*)sendSynchronousRequest:(NSURLRequest )请求returningResponse:(NSURLResponse*)响应错误:(NSError**)错误,有没有机会我可以在之前取消240秒了?我想也许设置一个计时器来取消这个同步请求,但我甚至不确定它是否可能?我在想:
[self performSelector:@selector(cancelRequest:) withObject:myRequest afterDelay:myTimeOut];
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,如果请求已经发布,这将导致灾难,我无法确定.思考?有没有人试过这样做?这是同步通话.
你无法取消它.只是不要使用它而是使用异步调用.那些你可以轻松取消.
这似乎对我有用:
NSURL *url = [NSURL URLWithString:@"http://someurl.com"];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];
NSHTTPURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
if (response == nil) {
// timed out or failed
} else {
// all good
}
Run Code Online (Sandbox Code Playgroud)
Ofcourse将超时间隔设置为您希望它在超时之前阻止主线程的时间 - 上述代码在5秒后成功超时
在iOS6和iOS5.1中测试过
| 归档时间: |
|
| 查看次数: |
5897 次 |
| 最近记录: |