NSURLSessionDownloadTask keeps retrying when using Background Configuration

后端 未结 4 1568
礼貌的吻别
礼貌的吻别 2021-02-04 17:08

I have a problem when it comes to a slow backend and downloading data with background configuration.

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConf         


        
4条回答
  •  伪装坚强ぢ
    2021-02-04 17:34

    Could not stop myself, here is your answer a bit refactored :)

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    NSURLSessionConfiguration *sessionConfig;
    float timeout = 5 * 60.0f;
    
    BOOL iOS8OrNewer = [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
    if (iOS8OrNewer) {
        sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
        request.timeoutInterval = timeout;
    }
    else {
        sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
        sessionConfig.timeoutIntervalForRequest = timeout;
    }
    
    _backgroundSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
    
    NSURLSessionDownloadTask *downloadTask = [_backgroundSession downloadTaskWithRequest:request];
    

提交回复
热议问题