[Solved] Upload photos with NSURLSession in background


From the comments above, it seems like you’ll be uploading several photos at a time and for that a “fromData” approach does not seem like a good idea.

Firstly, high resolution photos have several megabytes in size. If your loading a 5mb photo into memory, using NSData, you’re probably fine. But loading 10 photos would then mean you’d need 50mb of memory to hold them all.

Secondly, uploading large chunks of data, such as photos, is also a computationally intensive task. You should most definitely not try to upload them all at the same time. This means you’re going to have to build an upload queue that only starts uploading the next photo once the last one has finished. What happens then if internet connectivity is lost midway through the process? And what happens if the user closes the app all of the sudden?

Thus, I’d suggest you grab all the images’ data and write them to a temporary folder somewhere. Then you start the upload process, which would find a file on the temporary folder, upload it, delete it once the upload finishes successfully and start over with the next file.

This way you’re minimising your app’s memory footprint, making it more resilient in the face of the many adversities an app may face (no internet connection, being prematurely closed by the user and so on) and also abiding by the APIs that are provided to you.

2

solved Upload photos with NSURLSession in background