如何设置 NSURLConnection 请求的实际超时时间?
请查看以下代码: 下面的
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:<...> cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3.0];
<...>
[NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror]
代码是从后台线程调用的:
[self performSelectorInBackground:@selector(<...>) withObject:nil];
有时 sendSynchronousRequest 的工作时间远远超过 3.0 秒。 (约1分钟)。
- 如何设置真正的超时而不是不工作
timeoutInterval:3.0
? - 如何添加用户通过“取消”按钮随时停止冻结
NSURLConnection
请求的功能?
非常感谢您的帮助!
Please look to the following code:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:<...> cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3.0];
<...>
[NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror]
This code bellow is called from a background thread:
[self performSelectorInBackground:@selector(<...>) withObject:nil];
Sometimes sendSynchronousRequest works much more than 3.0 sec. (abount 1 minute).
- How to set real timeout instead not working
timeoutInterval:3.0
? - How to add an ability to user to stop freezing
NSURLConnection
request by Cancel button in any moment?
Thanks a lot for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,除了您调用的初始化程序之外,您还可以在 NSMutableURLRequest 上调用 -setTimoutInterval: 。超时值确实有效,但任何小于 240 的值都会被忽略——这是 iOS 框架遵循的最小值。如果您想设置较低的超时值,那么您唯一的选择就是使用异步请求。
如果您想异步取消请求(即,在后台执行请求并允许前台 UI 线程发出取消响应用户点击“取消”或“停止”按钮),那么您必须进行异步网址请求。没有办法通过同步请求来做到这一点。例如,当调度队列当前正在执行块时,您甚至无法杀死它。
您可能想看看 ASIHTTPRequest,它以稍微不同的方式封装了其中一些功能。
Note that in addition to the initializer you called, you can also call -setTimoutInterval: on an NSMutableURLRequest. The timeout value does indeed work, but any value less than 240 is ignored -- that's the minimum value respected by the iOS framework. If you want to set a lower timeout value, then your only choice is to use an asynchronous request.
If you want to asynchronously cancel a request (ie, perform the request in the background and allow the foreground UI thread to issue a cancel in response to the user hitting a Cancel or Stop button), then you have to make an asynchronous URL request. There's no way to do it with a synchronous request. For example, you can't even kill a dispatch queue while it is currently executing a block.
You might want to look at ASIHTTPRequest, which wraps up some of this functionality a bit differently.
超时间隔只是描述连接在超时之前任意时刻可以空闲的时间量。来自苹果文档:
因此,如果您的数据正在检索大量数据,则需要更长的时间。
如果您想更好地控制“停止监听”之前等待的时间,那么您应该使用异步请求。异步请求也会使用户在等待时不会看到冻结?
The timeout interval just describes the amount of time the connection can be idle at any point before it times out. From the Apple documentation:
So if your data is retrieving any significant amount of data, then it will take longer.
If you want to have more control over how long you wait before you "stop listening" then you should use an asynchronous request. An async request will also make it so the user doesn't see a freeze while waiting?