tezvyn:

Explain the roles of ViewModel, Repository, and data source in MVVM

Curated by the Tezvyn teamSource: developer.android.combeginner
Explain the roles of ViewModel, Repository, and data source in MVVM

Tests your grasp of unidirectional data flow and separation of concerns in Android MVVM. A strong answer maps ViewModel to UI state, Repository to data coordination, and Room or Retrofit to I/O.

WHAT THIS TESTS: The interviewer wants to see if you can draw clean architectural boundaries in Android MVVM. Specifically, they care whether you understand that each layer owns a single responsibility and that dependencies point inward toward data, not outward toward the UI. They also want to hear how state flows up and events flow down.

A GOOD ANSWER COVERS: First, the ViewModel acts as a state holder and transformation layer. It receives raw data from the Repository, converts it into UI state, and survives configuration changes. It never talks to Retrofit or Room directly. Second, the Repository serves as a single source of truth and abstraction boundary. It mediates between different data sources, for example returning cached Room data immediately while triggering a Retrofit refresh in the background. Third, the data source does the actual I/O. Room handles local persistence and SQL mapping; Retrofit handles network serialization and HTTP calls. Fourth, the interaction pattern is unidirectional. The UI observes ViewModel state; the ViewModel calls Repository methods; the Repository decides which data source to query; the data source returns results back up the chain. Fifth, testing implications. ViewModels are tested with fake Repositories; Repositories are tested with fake data sources; and data sources contain the only truly slow I/O.

COMMON WRONG ANSWERS: A major red flag is describing the ViewModel as a place for business rules or direct network calls. Another is claiming the Repository is just unnecessary boilerplate that can be skipped in small apps. Some candidates blur the lines by saying Room lives inside the ViewModel or that the Repository emits UI-specific strings. Confusing the Repository pattern with a DAO is also common.

LIKELY FOLLOW-UPS: The interviewer may ask how you handle errors across these layers, where coroutines and Flow fit in, or how you inject these dependencies with Hilt. They might also ask what happens if the Repository needs to merge local and remote data, or how you prevent memory leaks when the ViewModel outlives the UI.

ONE CONCRETE EXAMPLE: Imagine a news app. The user opens the headlines screen. The Fragment observes a UiState sealed class from the ViewModel. The ViewModel asks the NewsRepository for headlines. The Repository checks Room for cached articles and returns them immediately via Flow, then calls Retrofit to fetch updates. Retrofit returns JSON that the Repository maps to entities and writes back to Room. Room emits the new cache, the Repository passes it along, and the ViewModel maps it to UiState.Success. The Fragment simply renders what it receives.

Source: developer.android.com

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Explain the roles of ViewModel, Repository, and data source in MVVM · Tezvyn