Search code examples
androiddatabaseauthenticationsynchronizationandroid-contentprovider

What are the standard approaches to authorisation and syncing of a local Android DB to a server


I am new to Android and am about to write an application that allows a user to edit some data offline and then sync it to a master database on a server somewhere when online.

I am assuming that in order for the user to be able to work offline, I will need to have a database on the Android device which can be updated via a content provider / database adapter class.

My question is, what are my high level options for authenticating and synchronising my local database with the server database? Are there any common/recommended approaches?. I have heard of using RESTful APIs on the Server (but I do not know much about them) - is this a standard approach?.

Also, is there a common approach to how often the sync is done - e.g. periodically? immediately upon a change when online?.

Any help would be much appreciated as this is very much a grey area for me.


Solution

  • it is a little hard to answer all these questions since it really depends upon the application and how you want it to work. But, I would definitely look into RESTful web services. There are a number of good tutorials about this on the web, but basically you have to set up a Web Server that can take parameters in the URL and sometimes data in the body of a POST. For your case it will likely be a POST with data so you can send the update to the DB.

    But, there are some other problems, of course synching has to be done both ways. What if the data was changed on both the server and the mobile client? How do you do conflict resolution there?

    Second, as to the question of how often to sync - that is really up to you and how time critical is the data. Of course the longer you wait between syncs, the more stale the online data will be and the more likely it will be modified in both places. Syncing periodically is probably a good start and then adding in the sync when connectivity is back if you find that several sync periods have gone by without being able to connect - like if the phone is in airplane mode for example. But when syncing periodically, you need to be very mindful of data usage and battery so select a period that is reasonable - perhaps once an hour? And only sync if changes were made.

    Sorry I don;t feel like I can give more detailed info without knowing your actual use case for the data.