Better Control of Background URLSession + UploadTasks

I am trying to implement (in Swift/iOS) Resumable Uploads in a similar fashion to this wwdc23 video

Fortunately there is an open source solution to do most of the work! The issue is that background URLSessions seem to have a mind of their own?

Basically the gist of the resumable uploads is that an upload will progress to the best of its ability, and then when it fails it's supposed to run a HEAD request to see how much of the file has been written, and then resume uploading from the last good offset.

I can not for the life of me get this to work in a "real life" scenario. Something as trivial as leaving wifi for cell causes everything to break. And this isn't that crazy of a scenario! It's totally reasonable to expect a user to do some stuff before they leave the house, and then take their phone and drive away. The WiFI connection will drop and the user will switch to cell.

As it is right now the way URLSession handles things is that the initial task fails, but instead of loud, hard failing it quietly, softly fails and then as soon as there is network again the request is completely restarted. This breaks everything. Either the original request timed out and attempting to re-write the entire file fails because the offsets dont match any more (prev had some non-0 offset, new has 0 offset, non-zero != 0), or it fails because there are now two requests who think they have the appropriate offsets and everything gets out of sync.

My understanding is that iOS 17 has implemented resumable uploads in a nice way, but how to manage for all the users who don't immediately upgrade? Is there a way to say "Hey URLSession, I know you want to immediately restart this request that failed due to the network disappearing and normally this is what people want, but maybe just cancel that last request that failed and then run this little handler to see where you should really start from and then start from there"

My guess is that this is impossible because it needs to run in the background and iOS doesn't like letting apps run when they're not supposed to be? That's why this all had to be baked into iOS17.

Just looking for anyone who has any idea how to handle this.

Replies

My understanding is that iOS 17 has implemented resumable uploads in a nice way, but how to manage for all the users who don't immediately upgrade?

My guess is that this is impossible because it needs to run in the background

Right, for apps iOS 16 and lower there is no baked in solution for resumable uploads. This would have to be done by your own app's internal logic that is working with a custom server that you control.

Resumable uploads are supported on iOS 17 for background sessions. Are you having trouble with mobility (switching from one interface to another) issues while running in a background session or are there other issues taking place?