When handling data synchronization there are essentially three states that exist for most mobile applications: server state, session state, and screen state.
This is the state of data as it exists on the server. Most mobile apps will need to pull data from a remote database over the network and/or push local changes to the server. More than one application may push or pull to a single remote datastore, so it must handle multi-threading and conflict resolution. The server state is often considered the source of truth.
The session state is a local copy of data maintained client-side by the application. This can be stored either in-memory or in a local database and represents the current state on the client. Session state can become the source of truth when handling local mutations and/or offline mode.
Screen state is the copy of data as it is currently displayed on the device to the user. The UI typically consists of some visual representation of the session state, but also may provide a vehicle for the user to initiate new changes (i.e. user input).
Synchronization between server state and session state can be achieved via asynchronous data fetch (or post), push notifications, or a persistent connection like web sockets. One of the primary concerns here is client-side caching. Hint: This is the other hard thing in computer science.
Synchronization between session state and screen state can be achieved via data binding, observables, or user initiated refresh. It’s also important to communicate this type of synchronization to the user (progress bar, pull-to-refresh, timestamp, etc.)
One of the primary tasks of any clean architecture is effectively managing state synchronization. The most effective strategy will vary based on the individual application.
In the upcoming posts, we’ll take a closer look at the various data sync strategies and how they can be applied to MVP and MVVM on Android.
This post is part of a series on clean architecture that explores how classic software design principles can be applied to modern Android development.
If you found this article helpful, please give it some applause 👏 to help others find it. Feel free to leave a comment below.