nsurlSession

//当上传或下载数据成功时执行;当任务被取消时也会执行

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task

                            didCompleteWithError:(NSError *)error

 

 

/*

 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! 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.

 */

 

/*ios7时服务器不响应时会调用这个delegate。

  从ios8起,NSUrlSession的backgroud模式不会 在server没有响应时调用这个delegate。

  download/upload任务将会无限期的保持闲置。

  

  一般而言,后台模式下的NSURLSession 不会因为电缆的某些错误 而失败一个task。它会寻找一个合适的时机 继续执行这个request或重试这个request,直到resource timeout(即timeoutIntervalForResource属性设置的值),当前系统默认的值是一周。

   换句话说,在ios7里面使失败的行为是不对的。在background的上下文里,最好是不要因为网络问题而使task失败。所以,从ios8起,NSURLSession task会继续运行,直到时间超时或network loss.

 

   所以,timeoutIntervalForRequest在后台模式下不会生效,只有timeoutIntervalForResource会生效。

*/

非官方文档上的解释,stackOverflow里一位苹果员工的解释

posted @ 2016-08-09 11:43  little_star  阅读(388)  评论(0编辑  收藏  举报