iosxcodensurlsessionnsurlsessionuploadtask

Stream Requests Uploads getting slow in iOS Background Session from iOS 15.x


I am working on an app which uses iOS NSURLSession background session to upload the data to the S3 server. From iOS 15.x, We have observed that transfer speed has become too slow (~10 kbps).

Following is the configuration, I am using

  1. Create the NSURLSessionConfiguration using backgroundSessionConfigurationWithIdentifier
  2. Set HTTPMaximumConnectionsPerHost to 8
  3. Set timeoutIntervalForRequest to 60s
  4. Set timeoutIntervalForResource to 86400s (1 Day)
  5. Set discretionary to false
  6. Set TLSMinimumSupportedProtocolVersion to tls_protocol_version_TLSv12
  7. Create the NSURLSession using sessionWithConfiguration
  8. send request using uploadTaskWithRequest

When I tested with iPadOs 14.8.1, there is no degradation in performance, but with iPadOs 15.3.1 and 15.5, I can see the performance degradation (uploads getting 6x slow).

When I create the session using ephemeralSessionConfiguration upload is very fast and there is no degradation (working as before).

For all the tests, I am keeping the app in foreground only.

I have few queries:

  1. Is there any changes in background session configuration for iOS 15.x and greater ?
  2. We are currently using backgroundSessionConfigurationWithIdentifier to create background sessions. Should we consider moving to background tasks introduced in iOS 13 ?
  3. Is it upto the OS (in this case iOS) to schedule the requests and we (clients) has no or very little control over the transfer speeds ?

PS: The degradation is happening with iOS simulators also. In case of simulators, somehow it is dependent on macOs versions (as seen from my tests).

On macOs 12.4, in xCode 13.2, iOS 12.x, 13.x, 14.x, 15.x simulators are showing the performance degradation. When the same app is compiled from macOs 11.4, xCode 12.4, iOS 12.x, 13.x, 14.x are not showing any performance degradation.

Any inputs would be highly appreciated.


Solution

  • Having struggled with this problem myself, I wanted to share the solution - for others, and also for future me when I run into this again and inevitably forget: to fix slow download speeds for uploads using a background session configuration, make sure you configure the request's network service type - this setting works for me:

    var request = URLRequest(...)
    // configure request
    request.networkServiceType = .responsiveData
    

    .responsiveAV also worked for me. Do make sure you are only setting this if it is important that uploads happen in a timely manner - if it isn't important just hand it off to the background session with an appropriately configured timeout and let it get on with it.

    Note: this will only have an impact if the upload task is started while the app in the foreground. If the upload task begins when the app is already in the background (for example, you need to do some other work first with some extended background execution time and then resume the upload task) then it will ignore this and behave as if you had set it to .background which will be as slow as before as requests started in the background are heavily throttled.