Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use NSURLSessionUploadTask for offline syncing tasks?

I need something similar to Facebook's offline post capabilities. Basically I want users to create content locally on the device regardless of connection state, and whenever internet becomes available it should POST/PUT to the server.

I've searched the internet for a solution and I found that NSURLSessionUploadTask can be used for POST-ing in the background. But I couldn't figure out if the following scenarios are supported:

  • Will my task remain in the background queue when the user is offline and will the operating system try to execute items in the queue upon reconnecting with a network?
  • What happens if the application is force-closed by the user or crashes?
  • What happens if the operation fails?
like image 626
Hless Avatar asked Jun 12 '15 13:06

Hless


1 Answers

First of all, background NSURLSession allows file upload only. If that is ok for you:

  • The task will be in the queue until it receives a server answer.
  • If your app is force-closed, the task will still be executing. When the request is done, your app will be launched in non-interactive background state and receive application:handleEventsForBackgroundURLSession:completionHandler:. After you process the signal and call the completion handler or 30 second timeout, the app will be closed.
  • I the operation fails, you will receive URLSession:task:didCompleteWithError:

There is a good tutorial on background NSURLSessions. I suggest you to read all 4 parts of this great article.
If file upload is not an option for you, i suggest you to save information into local database and then wait for internet is reachable. (a good approach here is use of Reachability library, Alamofire allows to do that too). When internet becomes available, simply call your http requests with saved data.

like image 200
Sega-Zero Avatar answered Oct 23 '22 00:10

Sega-Zero