nsurlSession

//當上傳或下載數據成功時執行;當任務被取消時也會執行ios

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task服務器

                            didCompleteWithError:(NSError *)error網絡

 

 

/*session

 Since iOS8, the NSUrlSession in background mode does not call this delegate method if the server does not respond. this

 -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)errorrest

 

 The download/upload remains idle indefinitely. This delegate is called on iOS7 with an error when the server does not respond.server

 

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

 

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

 */it

 

/*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裏一位蘋果員工的解釋

相關文章
相關標籤/搜索