没有连接时,具有后台会话配置的 NSURLSession 不会返回错误
NSURLSession with background session configuration is not returning an error when there is no connection
当我使用后台会话配置设置 NSURLSession/Alamofire.Manager 时,如果没有互联网连接,我希望收到通常的 NSError
"Error Domain=NSURLErrorDomain Code=-1009 "互联网连接似乎处于离线状态。"。
如果我不使用后台配置,这种情况经常发生,但如果我进行这样的配置,我的 callback/delegate 方法将永远不会被调用。当我再次激活 wifi 时,它最终会被调用。
我希望立即收到错误消息。我错过了什么吗?
API 不会告诉您网络不存在,因为那将是一个错误,表明连接永远不会完成。实际上,假设网络最终会恢复正常。
如果您出于某种原因需要接收错误,请不要使用后台会话。
或者,如果您只是想知道网络是否正常运行以获取某种 UI 提示,请使用可达性 API。话虽如此,但不要拒绝基于失败的可达性启动请求,因为可达性是谎言。
后台session任务中的网络故障没有return报错的原因是:
In general an NSURLSession background session does not fail a task if
something goes wrong on the wire. Rather, it continues looking for a
good time to run the request and retries at that time. This continues
until the resource timeout expires (that is, the value in the
timeoutIntervalForResource property in the NSURLSessionConfiguration
object you use to create the session). The current default for that
value is one week!
我在 developer forum 找到了上面的答案。
可能对后台会话有帮助的更多详细信息:
Another benefit is that in a background session, we monitor the
network and power environment for you. This means that we cover things
like network reachability and connectivity for you, so you don't have
to use the reachability APIs at all. We won't attempt to establish a
connection until we know that the server is reachable. And similarly,
if the user is performing a download and steps out of Wi-Fi, normally
that task would then fail with a transmission error. But, in a
background session, we'll actually recover from that automatically and
retry it and resume where we left off if the download is resumable.
And you won't hear about that error.
来源:WWDC 2014
当我使用后台会话配置设置 NSURLSession/Alamofire.Manager 时,如果没有互联网连接,我希望收到通常的 NSError
"Error Domain=NSURLErrorDomain Code=-1009 "互联网连接似乎处于离线状态。"。
如果我不使用后台配置,这种情况经常发生,但如果我进行这样的配置,我的 callback/delegate 方法将永远不会被调用。当我再次激活 wifi 时,它最终会被调用。
我希望立即收到错误消息。我错过了什么吗?
API 不会告诉您网络不存在,因为那将是一个错误,表明连接永远不会完成。实际上,假设网络最终会恢复正常。
如果您出于某种原因需要接收错误,请不要使用后台会话。
或者,如果您只是想知道网络是否正常运行以获取某种 UI 提示,请使用可达性 API。话虽如此,但不要拒绝基于失败的可达性启动请求,因为可达性是谎言。
后台session任务中的网络故障没有return报错的原因是:
In general an NSURLSession background session does not fail a task if something goes wrong on the wire. Rather, it continues looking for a good time to run the request and retries at that time. This continues until the resource timeout expires (that is, the value in the timeoutIntervalForResource property in the NSURLSessionConfiguration object you use to create the session). The current default for that value is one week!
我在 developer forum 找到了上面的答案。
可能对后台会话有帮助的更多详细信息:
Another benefit is that in a background session, we monitor the network and power environment for you. This means that we cover things like network reachability and connectivity for you, so you don't have to use the reachability APIs at all. We won't attempt to establish a connection until we know that the server is reachable. And similarly, if the user is performing a download and steps out of Wi-Fi, normally that task would then fail with a transmission error. But, in a background session, we'll actually recover from that automatically and retry it and resume where we left off if the download is resumable. And you won't hear about that error.
来源:WWDC 2014