ios – 具有后台会话配置的NSURLSession在没有连接时没有返回错误

前端之家收集整理的这篇文章主要介绍了ios – 具有后台会话配置的NSURLSession在没有连接时没有返回错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我使用后台会话配置设置NSURLSession / Alamofire.Manager时,如果没有互联网连接,我希望收到通常的NSError“Error Domain = NSURLErrorDomain Code = -1009”Internet连接似乎处于脱机状态. .

如果我没有使用后台配置,这种情况经常发生,但如果我进行这样的配置,我的回调/委托方法永远不会被调用.当我再次激活wifi时,它最终会被调用.

我希望马上收到一个错误.我错过了什么吗?

解决方法

为什么后台会话任务中的网络故障不返回任何错误的原因是:

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

猜你在找的iOS相关文章