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
Mostly. There’s a few points I want to clarify…Would really appreciate knowing if what I postulated above is correct or not.
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.Obviously no more progress delegate calls (no delegate!)
The system resumes (or relaunches) your app in the background when the last task in your background session completes.When upload tasks finished, the system will message me
There’s actually two cases here:If the task comes back into the foreground during the upload, I can reconstitute the session config
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.
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"