码迷,mamicode.com
首页 > Web开发 > 详细

nsurlSession

时间:2016-08-09 12:11:09      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

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

- (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里一位苹果员工的解释

nsurlSession

标签:

原文地址:http://www.cnblogs.com/wyqfighting/p/5752651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!