Mobile App Offline Data Synchronization – Architecture
Many mobile apps are data-centric and are designed to replace pocket atlases, and dictionaries, as well as create new smart digital pocket references for data that changes seamlessly by leveraging different synchronization technologies.
In today’s world, the major challenge in mobile computing is to provide the same user experience to customers during poor or no network connectivity as with LTE or Wi-Fi network. Mobile users keep on roaming to different places, and oftentimes, they receive poor cell coverage and bad network connectivity. Most of the smartphone apps fail to work in such conditions since they are engineered to rely heavily on strong data connectivity.
The contexts causing the use of a data synchronization mechanism can be considered from two viewpoints: uploading and downloading. Uploading is the transfer of data from the mobile device to a remote backend server, whereas downloading is the transfer of data from the remote backend server to mobile devices. In both cases, the success or failure of the data sync should be conveyed to the user either directly (with a notification or dialog), or indirectly (in a log), with appropriate error information if a failure occurs.
Data synchronization mechanisms in mobile apps should address key questions such as-
- When should an application synchronize data between a device and a remote server?
- What should be the sync frequency so that it doesn’t drain out the user’s battery & network bandwidth?
- Should data sync be performed in the synchronous or asynchronous mechanism?
Data Synchronization on the mobile device can be performed in 2 ways –
- Asynchronous Data Sync –
The intent is to manage a data synchronization event asynchronously and without blocking the user interface.
- Synchronous Data Sync –
The intent is to block the user interface while data sync is happening in the background. The architecture of mobile data synchronization can be described in the following figure. Sync Service is responsible for syncing data from a remote server to a mobile device and stores the data locally in the SQLite database. All UI components are rendered via the local database. Now even in case of subsequent sync failures, it will provide the same user experience to customers.
The data synchronization in Android apps can be achieved using Sync Service and Sync Adapter. A Sync Adapter is a plug-in that handles background syncs on the Android platform to sync data from your app to a server. This plug-in is registered on the platform’s Sync Manager, which is in charge of running it and can be triggered when needed, requested, or scheduled.
In Android, Sync Adapter has many advantages over other approaches:
- Battery Efficiency
The system schedules the sync to run when other syncs run, or when some other network request was already made on the device. This prevents waking the device from its sleep for performing single sync.
- User Settings
All sync adapters on the device can be accessed from the Settings screen, under the account they are tied to. This gives the end-user the option to change sync preferences, see if there are any sync problems, or even disable the sync.
- Data Responsiveness
If we use the Content Provider for accessing/manipulating our data, the sync adapter can observe any changes done to it. That way, it can run only when the data actually changes.
- Retry Mechanism
The sync manager has its implementation to retry failed syncs, using timeouts and exponential backoffs.
I hope our article will motivate you to design your mobile application differently: offline-first instead of online-only. Importantly, it would move your app to the next level of the user experience and can improve the amount of time a user spends with your application.