强制同步调用 NSUrlConnection 超时

发布于 2024-10-19 20:03:05 字数 485 浏览 4 评论 0原文

我知道有关 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 技术交流群。

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

发布评论

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

评论(3

泪之魂 2024-10-26 20:03:05

您无法取消它。干脆不使用它并使用异步调用。您可以轻松取消这些。

You cannot cancel it. Simply don't use it and use an asynchronous call instead. Those you can easily cancel.

最初的梦 2024-10-26 20:03:05

这似乎对我有用:

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
}

当然,将超时间隔设置为您希望它在超时之前阻塞主线程的时间 - 上面的代码在 5 秒后成功超时

在 iOS6 和 iOS6 中进行了测试iOS5.1

This seemed to work for me:

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
}

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

海风掠过北极光 2024-10-26 20:03:05
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url    cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];

webData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

if (webData==nil) {
        [self displayAlert:@"Time Out" message:@"Request Timed Out"];
    }

正好 10 秒超时。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url    cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];

webData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

if (webData==nil) {
        [self displayAlert:@"Time Out" message:@"Request Timed Out"];
    }

Timeout in exactly 10 seconds.

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