7

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?
Hless
  • 3,326
  • 20
  • 22

2 Answers2

2

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.

Noor
  • 2,071
  • 19
  • 28
Sega-Zero
  • 3,034
  • 2
  • 22
  • 46
  • This does answer the question. Was hoping there was a way to use background sessions as my private HTTP queue :D. I'm going a different route though and have a shot at trying to implement this with the background 'fetch' mode. – Hless Jun 22 '15 at 09:00
  • Background fetch is not a silver bullet I'm afraid. It is a nice feature, but iOS decides when the app may receive fetch signal, depending on users activity, so you may never receive it. Another approach is to periodically send silent push notification from server, so the app could be waken up and perform some actions in background. See [my other answer](http://stackoverflow.com/a/30834566/1254172) on how you can get benefits of this mode and a cornerstones of it. – Sega-Zero Jun 22 '15 at 11:45
0

We were running into connectivity issues with our internal apps, so we wrote a Swift framework that allows any network operations to be enqueued and sent whenever the device has access to the internet - https://cocoapods.org/pods/OfflineRequestManager. You'll still have to handle the network request itself within the object conforming to OfflineRequest, but it sounds like a good fit for your use case.

The simplest use case would look something like the following, though most actual cases (saving to disk, specific request data, etc.) will have a few more hoops to jump through:

import OfflineRequestManager

class SimpleRequest: OfflineRequest {
    func perform(completion: @escaping (Error?) -> Void) {
        doMyNetworkRequest(withCompletion: { response, error in
            handleResponse(response)
            completion(error)
        })
    }
}
///////
OfflineRequestManager.defaultManager(queueRequest: SimpleRequest())