Android: Single Source Of Truth Strategy (Offline-First)

4 min readFeb 15, 2023
Photo by Marc Beuret on Unsplash

As suggested by the name, with single source of truth strategy we are defining a place where to find and store our data, being sure that there is just one source accessible to the rest of our app. Also, it helps us to have a version of our data available offline.

Single Source Of Truth

This pattern centralizes all the data in one place, in our case a database implemented with Room (you can follow the guide in the official guideline to set your database).

First, we should observe the database checking if there are local data available, if yes we should show them and in the other case we should check if it is necessary to fetch for new data from network or not, it depends of course if it’s necessary to update the local data that we have saved on our database, or if by chance our database is currently empty.

In the case of outdated/empty data, we should fetch data from network (remote data), if the call has success we can update our database with the remote data and show the local data already update, in the other case we should show an error.

Result wrapping