Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 Background Synchronization (with NSURLSessionDataTask?)

Scenario:

As a user I am able to take (an unlimited amount of) photos and videos which are stored in the apps documents folder. Each of these media files gets a record within a Sqlite database with additional information (for exeample a caption). All this is possible to do completely offline.

Back online I get a dialog with a list of all the videos and photos I took and a button which starts an upload process.

Each file is uploaded after the other together with its metadata by making a multipart POST request to the server. The response of the server is stored together with the metadata in the Sqlite database (so there is no fire and forget).

Reliable solutions?

If I am reading and understanding this chart correctly, the most simple solution would be to wrap each of these uploads within a Task. Side effect: after 10 minutes every task would be cancelled, which becomes a problem by having a slow connection or very large files (for example a very long video).

The recommended way would be to use NSUrlSession/Background transfer service.

Which leads me to my question:

Is it possible to wrap multipart POSTs in NSURLSessionDataTasks and would this be reliable, even if the task is running longer than 10 minutes or the user is suspending the app?

As I am a Xamarin/C# guy I would really appreciate some sample snippets for a working multipart upload, even if it's in Objective-C ;-).

like image 782
asp_net Avatar asked Oct 20 '22 21:10

asp_net


1 Answers

Almost and... yes.

Background Transfer service works with NSUrlSessionDownloadTasks and NSUrlSessionUploadTasks only. Not NSUrlSessionDataTasks, as described here.

Other than this "basic" limitation, it's safe to use background transfer service with upload tasks.

The 10-minute-freepass-in-the-background no longer applies on iOS 7 (basically, it's there, but different), however, with NSURLSession and background transfer service you do not need it.

I've a blog post here for background transfer service, based on download tasks.

An important thing to note is that, starting a task basically means that it will actually start sometime and actually finish some other time. This depends on whether the device is on cellular or Wi-Fi and other factors which are (probably) only known to iOS (and Apple).

like image 65
Dimitris Tavlikos Avatar answered Oct 23 '22 23:10

Dimitris Tavlikos