NSURLSessionDownloadTask keeps retrying when using Background Configuration

后端 未结 4 1576
礼貌的吻别
礼貌的吻别 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:37

    I managed to solve it. I'm not saying my solution is the solution but it's a solution.

    The behaviour I'm experiencing is that iOS 7 and iOS 8 prioritises properties differently. I have two places to set these timeout properties, the NSURLSessionConfiguration and in the NSMutableURLRequest. iOS 7 couldn't care less about the requests property and iOS 8 couldn't care less about the configurations property. Right now my solution looks like this:

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

    I guess one could just not do the systemVersion checks and set both properties to the same value. Although I strongly believe that less code is more I believe even more strongly in not affecting states that does not need to be affected. However I would love to hear peoples opinions. And if someone has a better solution please share.

    Oh one more thing. The retrying part will keep happening until timeoutIntervalForResource reaches its default value of 7 days according to the documentation. I have decreased this to 10 minutes.

    sessionConfig.timeoutIntervalForResource = 10 * 60;
    

    I'm not saying it should be changed. This is a decision we made for our specific environment setup.

    Update

    We changed the timeoutIntervalForResource back to the default value of 7 days. We have customers in China for instance and some of them have really poor connection. A master limit of 10 minutes was just silly.

    Make sure to check out Sunkas answer for better code quality. My code snippet, however, is spread out over different classes so I can't reuse that approach 100 %.

提交回复
热议问题