Up-Sync and Down-Sync in Android?
Asked Answered
B

6

26

I am working on a Point of Sale application that needs to be very good syncing mechanism. We have Magento Database.The android device have SQLite local Db. Now we need to sync in the following way:

Local ------Sync To---------------> Server (Up Sync)

Server------Sync To---------------> Locals (Down Sync)

There are 2 things:

1) write-to (How to take care??)

For every change that i do on local ,it will directly sync my local to server

2) write-back (How to take care???)

Whenever there is a change in server, we need to sync all our locals with server.

So, the task is: to identify a server update

And sync our locals. Like there are 4 devices are running in a store and we have added one new customer through one device. Now i want that the three other devices local db also updated with the information about that customer and server also updated.

I heard about the background threads and run threads after a time interval. But what is the best way to do that which don't affect the application. Also the all Big Retail stores uses the syncing process. what they used for that ?

Any help is appreciated.

Bullivant answered 2/5, 2013 at 10:45 Comment(0)
L
13

It fully depends on you database structure...

you have DATABASE in LOCAL (device) and on SERVER

NOW

You need to have TIMESTAMP fieLd added to the TABLES which actually you want to keep in SYNC.

When ever you will make any changes on server the TIMESTAMP will be updated there and same will be the case for the local database also what you have to do is now.

Run a service in the background which will keep on comparing the TIMESTAMPS of LOCAL with that of SERVER.

Now you have to put condition that if TIMESTAMP of SERVER is newer to that of LOCAL then bring changes from SERVER to LOCAL,

and vice versa will be the condition to take changes from LOCAL to SERVER.

Further you have to decide how frequently you want to run this SERVICE.

ALTERNATIVELY:

You can make the table there on SERVER which will store LAST_SYNCHED date for particular device

Whenever you will login in you device (or any other particular event on which you want it to perform this) the server will check-

  • when this device was LAST_SYNCHED
  • then it will compare it to TODAYS DATE
  • and will check what upadets actualy happened between these dates and will send the changes to the LOCAL (device)

and vice versa for LOCAL (device) to SERVER

you have to play with TIMESTAMPS rest you can have your own logic how to structure the database.

I told you what I have have observed, when I have been a part of such similar project

EDIT

The above Process defines how to sync the devices with server I mean the strategy..

If you want your devices to get notified from server when to sync instead of hitting the WEB-SERVICE recurrently ..

You can make use of PUSH NOtification, GCM is one of them that send push notification to devices, you can integrate it to your project

Limy answered 6/5, 2013 at 7:19 Comment(0)
W
6

For syncing you need to handle following two situations.

  1. How and when to receive server updates
  2. How to identify local non-synced data

How and when to receive server updates:

For receiving updates, we can use GCM (Google Cloud Messaging). If any updates made in server, server sends a push message to all devices. Devices will receive that push and based on the message, devices will download the data from server. (I think this is better approach than continuous hitting service for some particular intervals like polling)

For receiving only updated data from server, server maintains modified_timestamp column for all tables. First time devices will send empty timestamp, so that server sends all data to the device with server timestamp. Device receives the new data and updates local db and saves the latest server timestamp. For next time to get server updates, device will send stored server timestamp then server will send only modified data after that timestamp only. For each response server sends server timestamp, devices needs to store that timestamp and needs to use while calling service.

How to identify local non-synced data:

For sending local updates, local db needs to maintain one 'isSynced' column in tables. If any row modified in local isSynced will be false, after successful syncing local data to server isSynced will be true. so that we can handle local data up to date with server.

Updated:

You can find more information on this developer link

Wandawander answered 13/5, 2013 at 5:24 Comment(0)
H
1

Have you considered using commercial solution?

http://www.mobeelizer.com/ seems like what you want to achieve. There are probably many other.

Note: no affiliation with that company.

Headland answered 12/5, 2013 at 18:18 Comment(0)
M
1

I would say that the problem statement is incomplete. In above described setup what is missing is what actually you are going to synchronise.

Usual case in POS is that there exist few indices (id,value,...) tables that shall be distributed from central server to the client devices. In most cases it is price list, stock list, etc. Those tables should rarely be modified on client devices (actually could but then has to be redistributed from central server and acknowledged by client devices).

The other direction tend to be also pretty straightforward on client device you generate bills or invoices. These are again local stuff that shall be propagated towards server. Thus you actually store them locally and at sync point dispatch them to the server. Later on you might receive your own items back from server as an acknowledge.

EDIT: to detect changes, on-write timestamps as mentioned above is a must.

So far above described is the data flow.

Next you have to move into solution domain and implement these rules. There is couple of sync approaches (i.e.SyncML). On the other hand keeping it simple rulez. Thus the main concern should be some kind of locking and queueing that makes the thing robust.

It could also use the agent based client, in such case each device has it own agent (could be replica of last known state of the device db) but I would consider this as an advanced feature that might come in future release:-)

Magnetograph answered 12/5, 2013 at 23:56 Comment(0)
C
0

I am also working on the sales app in which i have to my local goals to server and server goals to my local goals

My proceder is that when ever my app is started i get the latest data from my server of my all my member and update my local data base with this data and when ever i change data in my local data base also update on sever side

also i used a sync button which will fetch latest data from the server if my team member changes its goal or profile

Chilton answered 10/5, 2013 at 7:28 Comment(0)
A
-1

IF you want updated data on all the devices, why don't you use remote database only, why are you introducing local database for this.

For your case i will suggest you to work with only remote database directly so the things can be done real time.

Allergy answered 4/5, 2013 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.