Repository in Android’s MVVM architecture
What is Repository in Android’s MVVM architecture?
Repository is a class which purpose is to provide a clean API for accessing data. What that means is that the Repository can gather data from different data sources(different REST APIs, cache, local database storage) and it provides this data to the rest of the app. The other components don’t know where the data comes from, they just consume it. It also serves as a single source of truth. Its role is to keep the local database up to date with the newest fetched data from remote service so that the application can still provide its functionalities with bad Internet connection or no connection at all.
Let’s look at an example where the ViewModel asks the Repository for data which will be visualized in the View.
As we can see the Repository serves as a mediator between the ViewModel and the sources of data. The process of returning data is shown in the diagram.
First the ViewModel asks the Repository for data. The Repository checks what’s in the database and returns the persisted data to the ViewModel for further visualization in the View. While the Repository is getting data from the database, it also sends a request to the remote service in parallel (asynchronously). When the response is back, the Repository updates the database with the newest fetched data. Then this data is consumed from the ViewModel which transforms it in an appropriate way and returns it to the View.
Letzte Beiträge
Share :
Share :
Weitere Beiträge
6 Fragen an unseren Senior Consultant Dominique
Heute möchten wir euch unseren IT-Berater und Entwickler Dominique vorstellen. Er ist seit November 2020 Teil unseres Teams und Spezialist in Java, Spring, Microsoft und Enterprise.
Handling Events with Lambda Expressions
Lambda Expressions and Functional Interfaces are a new feature of Java 8 and the support provided for lambda expressions is only with functional interfaces.
View Model. Example
In this article we will go through the steps of creating a simple game screen. We will make it the traditional way without using View Model and we will see why it is absolutely wrong to persist data in the View.