Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync offline core data with the server when app have internet connection

I need to create offline module that will store all my requests and then when Internet connection will be available the module will make request one by one to the server.

Right now I am working with Core Data and AFNetworking 2.0, but I am working online. So the online algorithm is next:

  1. Create request
  2. Check internet connection
  3. Wait for the response
  4. Create object (record in db) based on JSON response

But offline algorithm has a small different:

  1. Create request
  2. Check internet connection
  3. Create proxy object (record in db)
  4. Listen to the internet connection
  5. Sync temp data with the server.

The main thing unique identifier and relationships which need to update after temp object will synchronized with the object on the backend.

My question is there already made solution how to sync offline data with the server?

Or maybe you have better algorithm it is also ok for me )

like image 340
Matrosov Oleksandr Avatar asked Mar 11 '14 13:03

Matrosov Oleksandr


People also ask

How offline Sync works?

How offline sync works. Your client code controls when local changes are synchronized with a data sync service. Nothing is sent to the service until there you push local changes. Similarly, the local store is populated with new data only when you pull data.


1 Answers

I would suggest the following flow:

  1. Implement a "request manager" that has a "private queue" context inside it
  2. When some module need to issue a request, it does so using the manager
  3. when a request is needed, the manager ALWAYS write it to the store (using its context) with a timestamp of creation date
  4. The manager will also listen to online/offline status changes
    1. When online status is detected, the managed query the store for pending requests and issue them one by one to the server
    2. when a new request is needed, the manager will act as described in (4.1) to prevent request starvation
    3. You can use a flag indicating if the manager is currently in operation (processing requests) so that a new request inserted will not imediatly trigger a fetch from the store
    4. reqests issued to the server might have their own context to write to the store so that they won't intrfere with the manager work
    5. When "offline" status is detected, the manager may cancel all active requests (they will be executed the next time online status is detected
    6. When a request if finished (commited to the server and local store), it is deleted from the store

Before activating the manager, you could query the store for pending requests and cancel/delete the ones that are no longer relevant.

like image 163
Dan Shelly Avatar answered Oct 18 '22 05:10

Dan Shelly