Sync Data Between Android App and Webserver

Sync data between Android App and webserver

I'll try to answer all your questions by addressing the larger question: How can I sync data between a webserver and an android app?


Syncing data between your webserver and an android app requires a couple of different components on your android device.

Persistent Storage:

This is how your phone actually stores the data it receives from the webserver. One possible method for accomplishing this is writing your own custom ContentProvider backed by a Sqlite database. A decent tutorial for a content provider can be found here: http://thinkandroid.wordpress.com/2010/01/13/writing-your-own-contentprovider/

A ContentProvider defines a consistent interface to interact with your stored data. It could also allow other applications to interact with your data if you wanted. Behind your ContentProvider could be a Sqlite database, a Cache, or any arbitrary storage mechanism.

While I would certainly recommend using a ContentProvider with a Sqlite database you could use any java based storage mechanism you wanted.

Data Interchange Format:

This is the format you use to send the data between your webserver and your android app. The two most popular formats these days are XML and JSON. When choosing your format, you should think about what sort of serialization libraries are available. I know off-hand that there's a fantastic library for json serialization called gson: https://github.com/google/gson, although I'm sure similar libraries exist for XML.

Synchronization Service

You'll want some sort of asynchronous task which can get new data from your server and refresh the mobile content to reflect the content of the server. You'll also want to notify the server whenever you make local changes to content and want to reflect those changes. Android provides the SyncAdapter pattern as a way to easily solve this pattern. You'll need to register user accounts, and then Android will perform lots of magic for you, and allow you to automatically sync. Here's a good tutorial: http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-1/


As for how you identify if the records are the same, typically you'll create items with a unique id which you store both on the android device and the server. You can use that to make sure you're referring to the same reference. Furthermore, you can store column attributes like "updated_at" to make sure that you're always getting the freshest data, or you don't accidentally write over newly written data.

Sync large amounts of data between mobile app and webserver

This is a complex question, as the answer should depend on your constraints:

  1. How often will data change? If it is too often, then the snapshot will get out of date really fast, thus apps will be effectively updating data a lot. Also, with the big volume of data, an application will waste CPU time on synchronization (even if user is not actively using all of that data!), or may become quickly out of sync with the server - this is especially true for iOS where Applications have very limited background capabilities (only small window, which is throttled) compared to Android apps.

  2. Is that DB read-only? Are you sending updates to the server? If so, then you need to prepare conflict resolution techniques and cover cases, in which data is modified, but not immediately posted to the server.

  3. You need to support cases when DB scheme changes. Effectively in your approach, you need to have multiple (initial) databases ready for different versions of your application.

Your idea is good in case there are not too many updates done to the database and regular means of download are not efficient (which is what you generally described: sending millions of records through multiple REST calls is quite a pain).

But, beware of hitting a wall: in case data changes a lot, and you are forced to update tens/hundreds of thousands of records every day, on every device, then you probably need to consider a completely different approach: one that may require your application to support only partial offline mode (for most recent/important items) or hybrid approach to data model (so live requests performed for most recent data in case user wants to edit something).

Sync Data between Android App and Server Couchbase Lite

You're in luck! The requirement you've described is exactly how Couchbase Mobile works.

Couchbase Mobile is comprised of two parts:

  1. Couchbase Lite: A stand-alone embedded database that you can use directly within your mobile app.
  2. Sync Gateway: a synchronization mechanism that securely syncs data between mobile clients and server.

So, back to your question. In order to achieve what you're looking for you would first create an instance of your Couchbase Lite database. From there, as you would expect, you can perform basic CRUD operations, query data, etc. You can find more information on the built-in capabilities here:

  1. iOS (Swift)
  2. iOS (Obj-C)
  3. Android (Java)
  4. Xamarin (C#)

Once you've created an embedded database you can start replication (synchronization) on it by using the Sync Gateway integration that comes with the Couchbase.Lite and Couchbase.Lite.Enterprise SDK component for iOS, Android, or Xamarin.

I recommend checking out the following tutorials for using Couchbase Lite and Sync Gateway:

  • iOS: Data Sync Fundamentals
  • Android: Data Sync Fundamentals
  • Xamarin: Data Sync Fundamentals

How many way to sync data between android app and server?

Following so is really helpful, please have a look - Sync data between Android App and webserver

You can also use Android sync adapter - Transferring Data Using Sync Adapters



Related Topics



Leave a reply



Submit