Ashfaqur Rahaman
Ashfaqur Rahaman

Reputation: 758

How to share and sync android sqlite database across devices when they are updated?

I am a novice android app developer. Currently I am developing an app which has a sqlite database. Users store some data in that database. Now I want to add an online synchronization / data sharing feature in my app - If a user update his/her device's database( that means store / delete data ) all other users of that app will get that update through internet( I mean their sqlite database will updated accordingly ). How can I do that?

I have searched about this and found about Sync Adapter. But I am still puzzled sync adapter would solve my problem or not. And how can I share sqlite database across devices.

Please give a newbie friendly answer. Any easy Tutorial / Documentation will be appreciated.

Thanks!

Upvotes: 3

Views: 8834

Answers (2)

Jim
Jim

Reputation: 10278

Databases are created on each device - specific to the storage system, etc.

To "share a database" you need to share the data. Essentially, it is an export/import process. So the data will export from one device and import into another.

You can do this by storing the data in JSON or CSV format. The export creates a file (or is done in-memory) then needs to be sent view the network (probably HTTP) to a server. From there another device can download and import it into the database.

The import process must account for possible data conflicts (duplicates, etc.) and possible different database versions/structures. This can get very complicated, very quickly. Develop as much as you can before release - otherwise you will have all kinds of synchronization issues.

SyncAdapter can simplify some of this process for you. It also may not do waht you need or expect. Here's more info on that:

Own sync adapter for Android?

Upvotes: 2

Yash Sampat
Yash Sampat

Reputation: 30601

Unfortunately, there is no "easy" solution to the problem of syncing data between server & devices. As you rightly said, SyncAdapter & ContentProvider are the way to go. These are indeed quite difficult to use & implement for novice developers.

An alternative way is provided:

1. Set a boolean flag to true whenever a user changes the data on their device (i.e. whenever a new entry is made in the DB).

2. Check if internet is ON. If it is, update this data on the server with the web service. Set the flag to false.

3. If internet is not available, let the flag remain true.

4. Create a BroadcastReceiver that gets activated whenever there is a change in network status (i.e. internet is switched ON or OFF). If internet is available and the flag is true, call the web service to update the data on the server. Set the flag to false.

5. The same BroadcastReceiver has another function: if internet is available, call another web service to get new data from the server. This should happen every time internet is available.

Upvotes: 3

Related Questions