Up-Sync and Down-Sync in Android?
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 andwill 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
For syncing you need to handle following two situations.
- How and when to receive server updates
- 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
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.