NSURLSessionTask never calls back after timeout when using background configuration

后端 未结 5 910
面向向阳花
面向向阳花 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:15

    Timeout for DownloadTask is thrown by NSURLSessionTaskDelegate not NSURLSessionDownloadDelegate

    To trigger a timeout(-1001) during a downloadTask:

    Wait till download starts. percentage chunks of data downloading will trigger:

    URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:

    Then PAUSE the whole app in XCode debugger.

    Wait 30secs.

    Unpause the app using XCode debugger buttons

    The http connection from server should time out and trigger:

    -1001 "The request timed out."

    #pragma mark -
    #pragma mark NSURLSessionTaskDelegate - timeouts caught here not in DownloadTask delegates
    #pragma mark -
    - (void)URLSession:(NSURLSession *)session
                  task:(NSURLSessionTask *)task
    didCompleteWithError:(NSError *)error
    {
    
        if(error){
    
            ErrorLog(@"ERROR: [%s] error:%@", __PRETTY_FUNCTION__,error);
    
            //-----------------------------------------------------------------------------------
            //-1001 "The request timed out." 
    //        ERROR: [-[SNWebServicesManager URLSession:task:didCompleteWithError:]] error:Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x1247c42e0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://directory.clarksons.com/api/1/dataexport/ios/?lastUpdatedDate=01012014000000, NSErrorFailingURLKey=https://directory.clarksons.com/api/1/dataexport/ios/?lastUpdatedDate=01012014000000, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}
            //-----------------------------------------------------------------------------------
    
    
        }else{
            NSLog(@"%s SESSION ENDED NO ERROR - other delegate methods should also be called so they will reset flags etc", __PRETTY_FUNCTION__);
        }
    }
    

提交回复
热议问题