Background URL Sessions upload slowly even in foreground

I am working on an app which is capturing photos and uploading them to a server.

I have followed the URLSession configuration to properly support background transfers. However, I am now noticing that my photos are uploading way slower, even when the user has the app open and is interacting with the app. This is undesirable because the changes need to be reflected in real time to the server when the user is active in the app.

Previously, when I was just using URLSession.shared.uploadTask with a completion handler, the photos uploaded right away in a matter of 1-2 seconds.

Now it is taking 3-4 minutes per photo to upload. The photos are roughly 3mb in size each. I have tried setting the isDiscretionary property explicitly to false to no avail.

Sometimes the users are without internet connection, which is why I wanted to use the background session (so that the uploads can be automatically retried by the system when the user connects back to the internet)

So, in summary, I want the functionality of the background uploading (and to let the system retry when the user transitions from offline to online) however I also need realtime uploading when the user is interacting with the app.

What is the preferred way to solve this problem?

I want the functionality of the background uploading … however I also need realtime uploading when the user is interacting with the app.

In theory, URLSession is supposed to boost the priority of background session work when the user brings your app to the front. I tested that at one point, but it was a long time ago (around the iOS 8 timeframe!). It’s possible that something has changed since then, or it’s possible that this has never been the case for uploads (I was only testing downloads).

If this isn’t work then, sadly, there isn’t an easy way for you to fix that. I recommend that you file a bug about this, because this priority boost makes a lot of sense. Make sure to:

  • Test using the process described in Testing Background Session Code. Testing this stuff from Xcode is never a good idea.

  • Include a sysdiagnose log taken while seeing the problem.

  • Install the Background Networking for iOS and CFNetwork for iOS debug profiles before you capture you sysdiagnose log. Get these from our Bug Reporting > Profiles and Logs page.

Please post your bug number, just for the record.


In terms of workarounds, there’s nothing that sounds particularly attractive. You could do something extreme, like cancel your background session uploads when coming to the foreground and resume them in a standard session, but that’s a lot of code. And it also only make sense if your server supports resumable uploads. And even then I’m not sure it’s a great idea given that your 3 MB-ish uploads are kinda small.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Background URL Sessions upload slowly even in foreground
 
 
Q