Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 - NSURLSession Resumable Uploads

I'm currently in the process of designing a system for uploading multiple images to a server from an application. The files will be stored on the local device and the current idea is to split them into chunks and then upload them chunk-by-chunk to the server, waiting for a success message back before uploading the next one.

This process should be seamless for the user, they should press a button to submit all images and then be able to close the application and for it to continue in the background. With iOS 7 I have seen that you can pass your uploads to the new NSURLSession API and have it deal with the upload task independent from your application.

I was wondering if anyone knows how reliable this transfer is. If I pass it an entire image, what happens if internet connection drops half way through? Does the background transfer service retry at a later time? The idea is that we can upload an image, and if it fails half way through that we can simply continue from that point later on when connection is resumed.

One idea we had was to split the files into chunks and then pass all the chunks as separate NSURLSessionUploadTasks and just assume that they'll all be sent to the server eventually.

Another was to send the first chunk using:

uploadTaskWithRequest:fromData:completionHandler:

And then in the completitionHandler to then send the next chunk. My issue with this is that if in the background the next chunk will have its

@property BOOL discretionary = true

which means that it will only send over WiFi when plugged in. I need this to work over all networks and be able to cope with interuptions.

I don't want any code, just advice on if this is the correct way to go about achieving this.

like image 696
Ryan Burke Avatar asked Oct 01 '13 10:10

Ryan Burke


1 Answers

I ll try to answer some of your questions

If your upload stops midway due to an error, your app should be launched in the background and you should be able to perform the required error handling. In your case, if you server supports resumable uploads, you should be able to get the offset and then setup another task to upload the next chunk.

If you are using a NSURLSession configured to run in the background then you cannot setup upload tasks with completion handlers and you cannot upload your chunks using a NSData object. Your only option is to write the chunks onto disk and then use

uploadTaskWithRequest:fromFile:

I think this makes sense because a NSData object resides in the application memory and background uploads are performed by an external daemon so it would only be right to upload files (from the user's sandbox). Also instead of using completion handlers you will need to make use of delegates to listen for the callbacks (while the app is in the foreground).

If you still prefer to use a NSData object, then an alternative solution would be to create a task using

downloadTaskWithRequest:

This way you can set the NSData object in the body of the request and pass that along in the request.You will be need to setup your request using NSURLMutableRequest to achieve this.

For the discretionary property, since it defaulted to TRUE i am not sure if we can do much about that. It says transfers are more likely to occur on Wi-Fi and when plugged in so there might be a small chance it might occur over cellular but don't take my word for it i am just guessing.

like image 178
lost found Avatar answered Oct 20 '22 03:10

lost found