Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Linking and syncing Room database to online server database

I am developing an app that uses a database to store the data on the server, but I am trying to save some of the data locally, and in the event of no internet connection being established, save new data locally to the device and synchronize any changes to the server when an connection is re-established. What is the best and most efficient way to do this?

I have been looking at Androids Room persistence library and it seems like the logical choice, but I am not sure how it goes about synchronizing changes to/from the local storage database. I have looked at multiple threads and forums for help, but have had no luck so far. Please help.

like image 376
aarond203 Avatar asked Nov 26 '22 13:11

aarond203


1 Answers

One way is to build your own sync adapter: https://developer.android.com/training/sync-adapters

You will need to handle most of the sync logic between the client and the server, but that allows you to use any database technology in the server. From the docs:

A sync adapter doesn't automate any data transfer tasks. If you want to download data from a server and store it in a content provider, you have to provide the code that requests the data, downloads it, and inserts it in the provider. Similarly, if you want to send data to a server, you have to read it from a file, database, or provider, and send the necessary upload request. You also have to handle network errors that occur while your data transfer is running.

A sync adapter doesn't automatically handle conflicts between data on the server and data on the device. Also, it doesn't automatically detect if the data on the server is newer than the data on the device, or vice versa. Instead, you have to provide your own algorithms for handling this situation.

like image 173
danb4r Avatar answered Nov 29 '22 05:11

danb4r