gpt4 book ai didi

ios - WiFi 关闭时 NSURLSessionTask 永远不会回调

转载 作者:行者123 更新时间:2023-11-29 00:29:03 25 4
gpt4 key购买 nike

当我关闭 WiFi 连接并在 iPhone 6s 10.2 模拟器上运行以下代码时,回调永远不会执行。我预计回调会很快触发,并出现“无互联网连接”之类的错误。

NSLog(@"request-start");
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:0];
task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"request-end");
}];
[task resume];

我的问题

我在应用程序首次加载时获取数据。如果 wifi 关闭,我需要显示错误。如果我设置超时,它会被遵守——但它需要超过 10 秒,我不想让他们等待。我也曾尝试通过可达性检测网络状态,但首次加载应用时网络状态通常为 unknown

最佳答案

timeoutIntervalForResource

This property determines the resource timeout interval for all taskswithin sessions based on this configuration. The resource timeoutinterval controls how long (in seconds) to wait for an entire resourceto transfer before giving up. The resource timer starts when therequest is initiated and counts until either the request completes orthis timeout interval is reached, whichever comes first.

The default value is 7 days.

timeoutIntervalForRequest

Important

Any upload or download tasks created by a background sessionare automatically retried if the original request fails due to atimeout. To configure how long an upload or download task should beallowed to be retried or transferred, use thetimeoutIntervalForResource property.

The default value is 60.

因此,如果没有设置超时,您的连接将运行 7 天。

In general an NSURLSession background session does not fail a task ifsomething goes wrong on the wire. Rather, it continues looking for agood time to run the request and retries at that time. This continuesuntil the resource timeout expires (that is, the value in thetimeoutIntervalForResource property in the NSURLSessionConfigurationobject you use to create the session). The current default for thatvalue is one week!

Source

关于ios - WiFi 关闭时 NSURLSessionTask 永远不会回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42353046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com