Problem:
I am building a photo organization/backup iOS app which requires users photos from their device galleries to be uploaded to our server.
User creates his profile and logs into the app and his gallery is scanned for images.The uploads happen in batches of around 10 files as multi-part form data requests. After each batch is uploaded, I need to make an api call to our server to commit the just concluded upload. The server could also be making its own commits like creating albums based on image content etc. after processing the batches previously committed. If that happens, the client gets an out of sync response from server while committing the upload and it has to first fetch the server commits before it commits the upload. Subsequently upload of the next batch of 10 images is scheduled.
I am using NSURLSessionUploadTask for this purpose. When the upload finishes while the app is in suspended state, the app gets woken up and I may be required to do either 1,2,3 or just 2,3 below
- Fetch commits done by the server for previous committed uploads
- Commit the upload that just finished
- Schedule next batch of 10 images for upload
====================================================
This isn't working for us. The app won't have enough time for executing 1,2,3 or 2,3 when it's woken up after the upload finishes. Requesting background execution time can't take is beyond 180 seconds because Apple caps it at that.
I cannot just keep uploading batches without committing them because the server has a buffer of 100 images before it starts overwriting images. If I continue uploading without committing, the server starts to overwrite previously uploaded images thus losing data.
Also I have observed that even if I tried uploading multiple batches of images without performing any commits, the first task I schedule as NSURLSessionUploadTask in the background as soon as the ongoing upload finishes is not picked up by the uploader deamon nsurlsessiond immediately. Sometimes it picks it up after 5 minutes, sometimes after 10. Through out the process, I am having a good Wifi connection and my NSURLSession is configured accordingly.
For the above I had read on StackOverflow that if the nsurlsessiond queue gets empty it doesn't pick the next task immediately. So just for experimenting I submitted 3-4 batches to upload and put the app into background but the behavior is the same. The second upload doesn't get picked up until 5-10 minutes and so on for 3rd and 4th.
====================================================
I have seen Google photos upload continuously without using location services. Dropbox also does it. Their commit architectures may be different from ours. But currently, I am not even able to get 3-4 batches of image to upload continuously even after ignoring the whole commit side of things where I have to make extra api calls.
Can you guide us in analyzing this situation so that I can come up with a solution for background uploads for our app.
Thanks again.