MVVM-2: The app should possess a single source of truth of data.


Description

In the context of mobile applications, consistency of data can become an issue. While caching mechanisms allow to save energy and bandwidth, multiple data sources can create inconsistencies and even conflicting Views. In order to avoid such issues, it is recommended to designate a dedicated component as single source of truth for the entire app. Specifically, in the context of MVVM, the Room persistence library is an official architectural component of Android which is specifically tailored for such task.

Example

We created a simple MVVM example.

In our example the viewModel contains only one database. Whenever someone creates a Repository, an instance of the NoteDatabase gets requested. This NoteDatabase is a singleton, so each repository will contain the same database.

To retrieve or insert data into the database, the noteDao has to be used.

Check this page to view the complete repository.