sina.rahimi
3 min readMar 17, 2018

Single source of truth

Based on the android developer guide:

In this model, the database serves as the single source of truth, and other parts of the app access it via the repository. Regardless of whether you use a disk cache, we recommend that your repository designate a data source as the single source of truth to the rest of your app.

What is the Single source of truth(SSOT) ?

In information systems design and theory, single source of truth (SSOT), is the practice of structuring information models and associated schemata such that every data element is stored exactly once.

with a single source of truth, you can have an offline app and be sure your data always use one source and that is your database.

Dependencies

Add these dependencies in module build.gradle

I separated each dependency that related to each other as a method so I can find them easily.

Retrofit2

Retrofit is the library that we use to fetch data.

Lifecycle

We use this library for ViewModel and LiveData.

Room

The room is the library for using the database. Before Room I used Realm but there is something that interesting about Room is its make your APK file much smaller than Realm (Something like 8Mb smaller).

There are three major components in Room: Database, Entity, DAO

Sample

This sample project that I used, fetches data from API and save them to a database. at first, its check the database and show what’s in it before that we request to the server and waiting for the response when the request is done and we get the response and save it to a database. Because we use LiveData always we know that if every change happens in our database we see that in our UI.

Packaging

The first thing you need to do is to know how to package your project. As you see my packaging looks like the picture above.

Model

I have two model SearchResult and Movie. You create your model based on your API. Movie Class is annotated with @Entity and name of the table. To make field primary key, you need to annotate a field with @PrimaryKey and property autoGenerate which assign automatic IDs. Room will create a movie table with defined attributes.

Dao

DAO (Data Access Object) defines the method that accesses the database, using annotation to bind SQL to each method.

I have a generic BaseDao and MovieDao, You don't need both of them just Movie Dao is enough.

AppDatabase

This abstract class that extends from RoomDatabase, we will define a list of entities and database version in it. This class is annotated with @Database annotation. It’s good practice to use the singleton approach for the database, so you need to create a static method which will return the instance of AppDatabase.

DataBaseHelper

I create the MovieDatabaseHelper to access my dao interface.

MovieAPI

This is why I use Single form RxJava, because Single is something like an Observable, but instead of emitting a series of values — anywhere from none at all to an infinite number — it always either emits one value or an error notification.

MovieRepository

The repository class is the main part of our sample. We have a method called getMovies that return LiveData. First, it returns the liveData from the database and in that time we send the request to the server when the request is done we save data to the DataBase And always you see the fresh Data; but from one source and that is your database(SSOT).

ViewModel

What is the ViewModel?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

Conclusion

This sample just for understanding the pattern and the idea of SSOT so I keep it simple.

You can check my repo on GitHub :

Please share with me what you think about and let me know if you have an idea.