NSURLSessionTask never calls back after timeout when using background configuration

后端 未结 5 908
面向向阳花
面向向阳花 2021-02-01 05:35

I am using NSURLSessionDownloadTask with background sessions to achieve all my REST requests. This way I can use the same code without have to think about my applic

5条回答
  •  星月不相逢
    2021-02-01 06:39

    Since iOS8, the NSUrlSession in background mode does not call this delegate method if the server does not respond. -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
    The download/upload remains idle indefinitely. This delegate is called on iOS7 with an error when the server does not respond.

    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 of the timeoutIntervalForResource property in the NSURLSessionConfiguration object you use to create the session). The current default for that value is one week!

    Quoted information taken from this Source

    In other words, the behaviour of failing for a timeout in iOS7 was incorrect. In the context of a background session, it is more interesting to not fail immediately because of network problems. So since iOS8, NSURLSession task continues even if it encounters timeouts and network loss. It continues however until timeoutIntervalForResource is reached.

    So basically timeoutIntervalForRequest won't work in Background session but timeoutIntervalForResource will.

提交回复
热议问题