如何设置 NSURLConnection 请求的实际超时时间?

发布于 2024-12-01 12:26:38 字数 642 浏览 0 评论 0原文

请查看以下代码: 下面的

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分钟)。

  1. 如何设置真正的超时而不是不工作timeoutInterval:3.0
  2. 如何添加用户通过“取消”按钮随时停止冻结 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).

  1. How to set real timeout instead not working timeoutInterval:3.0?
  2. How to add an ability to user to stop freezing NSURLConnection request by Cancel button in any moment?

Thanks a lot for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-12-08 12:26:38
  1. 请注意,除了您调用的初始化程序之外,您还可以在 NSMutableURLRequest 上调用 -setTimoutInterval: 。超时值确实有效,但任何小于 240 的值都会被忽略——这是 iOS 框架遵循的最小值。如果您想设置较低的超时值,那么您唯一的选择就是使用异步请求。

  2. 如果您想异步取消请求(即,在后台执行请求并允许前台 UI 线程发出取消响应用户点击“取消”或“停止”按钮),那么您必须进行异步网址请求。没有办法通过同步请求来做到这一点。例如,当调度队列当前正在执行块时,您甚至无法杀死它。

您可能想看看 ASIHTTPRequest,它以稍微不同的方式封装了其中一些功能。

  1. 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.

  2. 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.

云仙小弟 2024-12-08 12:26:38

超时间隔只是描述连接在超时之前任意时刻可以空闲的时间量。来自苹果文档:

如果在连接尝试期间请求保持空闲状态的时间超过超时时间
间隔,则认为请求超时。

因此,如果您的数据正在检索大量数据,则需要更长的时间。

如果您想更好地控制“停止监听”之前等待的时间,那么您应该使用异步请求。异步请求也会使用户在等待时不会看到冻结?

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:

If during a connection attempt the request remains idle for longer than the timeout
interval, the request is considered to have timed out.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文