URLSession background does not always work correctly and when opening the application again a white screen appears

I have a project that must upload data in background and keep data going even when the app is closed/terminated. So i have a background upload of data that should start/continue when the app is unloaded from memory (user force exit). In most of the cases after killing the app background upload resumes after some time (from few seconds to 10-20 mins) and data is successfully sent. However this does not always work even as expected.

  1. When the user kills the application, background uploading may terminate too with an error «Error Domain=NSURLErrorDomain Code=-999 "(null)" UserInfo={NSURLErrorBackgroundTaskCancelledReasonKey=0»
  2. After killing the app background upload may stop without finishing/throwing error and never produce any event, and when I open the application background upload does not resume. There is no error events and there is no pending/active background tasks either on my URLSession. Can they really vanish this way?
  3. In very rare cases I have encountered the problem that when background uploading in proccess (app is fully closed and when I launch the application a white screen appears (i see no splash screen, no main view), but I still can see logs that the background tasks is working actively. After the completion of data uploading the white screen does not go away, it is necessary to re-launcth the application to solve this problem.

I note that these cases are not always encountered and most of the time background upload works correctly. But I'd would like to understand how to handle these problems and what can cause this weirdness.

Do I get that right, so that after the user has force exited the application and it is unloaded from memory, even if a background upload task is launhed - we have no guarantee that the task will complete and the system won't kill the background task? I guess i have to act on my own, keep an eye on my tasks and restart them if they're got vanished/terminated? Is there any advices or good practices how to handle this best? And are there any strict limits on the amount of data that can be sent in background per task?

P.S I do have these capabilities enabled: Background fetch & Background processing.

The expected behaviour for URLSession background sessions is that, if the user ‘force quits’ your app [1], all the tasks in the background session will fail with NSURLErrorCancelled. The NSURLErrorBackgroundTaskCancelledReasonKey property of the error’s userInfo dictionary should contain a value of NSURLErrorCancelledReasonUserForceQuitApplication. You won’t ‘see’ this failure until the user next manually launches your app.

This is because the system treats the force quit gesture as a strong indication that the user doesn’t want your app running in the background. And this doesn’t just impact URLSession. Virtually all background execution subsystem will refuse to relaunch a force quit app until the user has manually launched it again.

Given that, this goal is not achievable:

I have a project that must upload data in background and keep data going even when the app is closed/terminated.

Your app will not be able to ‘keep data going’ after a force quit.

Share and Enjoy

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

[1] Technically this is swipes up to remove your app from the multitasking UI, because the term force quit implies that the app has an extant process and there’s no guarantee that’s the case.

URLSession background does not always work correctly and when opening the application again a white screen appears
 
 
Q