How to transition file upload when App backgrounded?

Situation: App has several huge files to upload to a server. User starts upload in foreground, get tired watching the slow progress, and backgrounds the app do web surf.

I've been looking at "tus" (resumable uploads) and coming up with all kinds of crazy schemes, and sitting back and rethinking this, I'm now wondering if I have overcomplicated the solution.

What appears now to be the simple and easiest solution is to create a background URL Session while in the foreground, set "isDiscretionary" to false (want uploads to start immediately), then create some upload tasks and using the session delegate, watch the progress.

If the app backgrounds, then iOS will continue them with "isDiscretionary" still true (subject to internal constraints). Obviously no more progress delegate calls (no delegate!)

When upload tasks finished, the system will message me. I can even add more tasks at that point if I want.

If the task comes back into the foreground during the upload, I can reconstitute the session config, add myself back as a delegate and then I'll start seeing the delegate session messages again.



The above may sound simple and self apparent, but really, I've read many articles on using background sessions and haven't seen the use case of initiating them in the foreground and having the app switch back and forth between foreground and background.

Would really appreciate knowing if what I postulated above is correct or not.



As an aside, I was going to use the "tus" Concatenation API, break the big file into chunks, start sending the chunks in foreground, if moved to the back, cancel any partial uploads, then re-submit uploads to a background task. Complex and if the above works, totally unneeded.

David
Answered by DTS Engineer in 627490022

Would really appreciate knowing if what I postulated above is correct or not.

Mostly. There’s a few points I want to clarify…

Obviously no more progress delegate calls (no delegate!)

You talk about being moved to the background or foreground, but you generally should be talking about being suspended and resumed. If you’re in the background and running for other reasons — for example, you’re an audio app that’s actively playing audio — you effectively act like you’re in the foreground.

When upload tasks finished, the system will message me

The system resumes (or relaunches) your app in the background when the last task in your background session completes.

If the task comes back into the foreground during the upload, I can reconstitute the session config

There’s actually two cases here:
  • Suspend > resume — In this case your background session will become active on the resume.

  • Suspend > terminate > relaunch — In this case you will, as you say, have to reconstitute your background session on the relaunch.

Share and Enjoy

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

Would really appreciate knowing if what I postulated above is correct or not.

Mostly. There’s a few points I want to clarify…

Obviously no more progress delegate calls (no delegate!)

You talk about being moved to the background or foreground, but you generally should be talking about being suspended and resumed. If you’re in the background and running for other reasons — for example, you’re an audio app that’s actively playing audio — you effectively act like you’re in the foreground.

When upload tasks finished, the system will message me

The system resumes (or relaunches) your app in the background when the last task in your background session completes.

If the task comes back into the foreground during the upload, I can reconstitute the session config

There’s actually two cases here:
  • Suspend > resume — In this case your background session will become active on the resume.

  • Suspend > terminate > relaunch — In this case you will, as you say, have to reconstitute your background session on the relaunch.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
How to transition file upload when App backgrounded?
 
 
Q