iosimage-uploadingnsurlsessionnsurlsessionuploadtask

Background upload of large amount of data


I need to upload large number of photos to a server from my iOS device. For example, 500 photos. How should I do this right?

I created upload task with background session configuration with NSURLSession for each photo. I tried making my own custom queue where each next task would launch after completion of previous one. But at one moment new task wasn't starting. I guess, because it all was happening in the background. More on this issue, you can read here

(If approach with a queue was right, would you please advise a good realisation of a queue for async tasks, because I may messed up something in my implementation)

So after article linked above, my guess was I should start all upload tasks at once (not one after another). But I have efficiency concern. If I would create a task for each photo, it would 500 background async task and about 1 gigabyte of a data uploading parallel. I guess, it would cause some problem with network.

To sum up all said above, which is the right way to upload large piece of data in background in iOS (500 photos in my case)?


Solution

  • Unfortunately, Apple's APIs are terrible at this task because of bad design decisions. There are a couple of major obstacles that you face:

    I suspect that the best approach is to:

    With the caveat that all of this must be done fairly quickly. You may find it necessary to pre-combine the files into ZIP archives ahead of time to avoid getting killed. But do not be tempted to combine them into a single file, because then you'll take too long when truncating the head on retry. (Ostensibly, you could also provide any parameters as part of the URL, and make the POST body be raw data, and provide a file stream to read from the ZIP archive starting at an offset.)

    If you're not banging your head against a wall already, you soon will be. :-)