Android Data Sync - Part 1

What is data synchronization?

"Data synchronization - is the process of establishing consistency among data from a source to a target data storage and vice versa and the continuous harmonization of the data over time." - Wikipedia

In current synchronization schema we need 4 layers:

  • Sync
  • Storage
  • Communication
  • User Interface

We will dive into layer details later, the most important is Sync layer - think about it as some kind of Service which perform operations in background.

Downloading data from server

Diagram below illustrate our sync process when we need to download data from server.

Step 1
Data synchronization can occur automatically (server send us GCM notification to tell that data on server has been updated) or manually (user perform pull to refresh which trigger synchronization).

Step 2
Sync service resolve which type of synchronization we need to do and perform requests to server, parse request result and save data to database.

Step 3
When synchronization is completed and database is updated, Sync service send global application even (via event bus or local broadcast) to tell that Table A, B and C was updated.

Step 4
User interface - Activity A, B and C are only displaying data from database. To know whenever data of Table A, B and C changes they are subscribed to global application events. When such event occurs, user interface reload data from database.

Uploading data to server

Diagram below illustrate our sync process when we need to upload data to server.

Step 1
When user change some data (update his profile) we need to save or update data in database and trigger synchronization.

Step 2
Sync service resolve which data we need to upload to server, parse request result and update data in database.

Step 3 (same as during downloading data from server)
When synchronization is completed and database is updated, Sync service send global application even (via event bus or local broadcast) to tell that Table A, B and C was updated.

Step 4 (same as during downloading data from server)
User interface - Activity A, B and C are only displaying data from database. To know whenever data of Table A, B and C changes they are subscribed to global application events. When such event occurs, user interface reload data from database.

What we know so far?

  • User Interface displays data from database only.
  • Sync service made requests to server, update database and send global application event.

In next part of this articles series we'll dive into layer details.

comments powered by Disqus