强制同步调用 NSUrlConnection 超时
我知道有关 ios 上 nsurlconnection 的讨论,并且超时至少有 240 秒。我的问题是,如果我通过 NSURLConnection 的 + (NSData *)sendSynchronousRequest:(NSURLRequest )request returnedResponse:(NSURLResponse *)response error:(NSError **)error 发送同步调用,是否有任何错误我可以在 240 秒结束之前取消此操作吗?我在想也许设置一个计时器来取消这个同步请求,但我什至不确定它是否可能?我在想:
[self performSelector:@selector(cancelRequest:) withObject:myRequest afterDelay:myTimeOut];
我有一种感觉,如果请求以某种方式被释放,这将导致灾难,而我无法确定这一点。想法?有人尝试这样做吗?这是一个同步调用。
I am aware of discussions regarding nsurlconnection on ios, and that there is a minimum of 240 seconds for a timeout. My question is, if I am sending a synchronous call via NSURLConnection's + (NSData *)sendSynchronousRequest:(NSURLRequest )request returningResponse:(NSURLResponse *)response error:(NSError **)error, is there any chance I can cancel this before the 240 seconds is up? I am thinking perhaps setting a timer to cancel this synchronous request, but im not even sure if its even possible? Im thinking:
[self performSelector:@selector(cancelRequest:) withObject:myRequest afterDelay:myTimeOut];
I have a feeling this will result in disaster if somehow the request has been released, and I would have no way to determine that. Thoughts? Has anyone tried to do this? This is a synchronous call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法取消它。干脆不使用它并使用异步调用。您可以轻松取消这些。
You cannot cancel it. Simply don't use it and use an asynchronous call instead. Those you can easily cancel.
这似乎对我有用:
当然,将超时间隔设置为您希望它在超时之前阻塞主线程的时间 - 上面的代码在 5 秒后成功超时
在 iOS6 和 iOS6 中进行了测试iOS5.1
This seemed to work for me:
Ofcourse setting the timeout interval to how long you want it to block the main thread before timing out - The above code successfully timed out after 5 seconds
Tested in iOS6 & iOS5.1
正好 10 秒超时。
Timeout in exactly 10 seconds.