Search code examples
androidoffline-cachingandroid-syncadapter

How to know if the data on the server is newer than the data on the device, or vice versa?


I have been trying to implement SyncAdapter to sync data between client(android device) and server using sqlite.The same data will be shared between different devices.

On Update: As soon as a particular record is updated on one client device it will be pushed to the server. At that moment the updated record should also be synced from the server to other connected devices.

Now, when we send sync request from other connected devices, what is the best way to know which record get updated on the server?


Solution

  • Here is what I would do to achieve this:

    • First, consider using timestamps. Here is how that could work;
    • In your database table (remotely), have a column updated_at which as soon as you sync your fresh data up, you should set a new timestamp for it.
    • In your android code, you can simply store lastSyncTime in your preferences then compare with what you get from your server column.

    You can easily pull the value from an endpoint /last_sync/ to see if the sync_time has changed.

    By comparing the two timestamps, you can then decide as to whether you want to sync down new data or keep the old until the new ones are synced.

    I hope this helps. Good luck!