Skip to content

(MVVM) Model View View Model

Devrath edited this page Nov 30, 2023 · 10 revisions

Improvement from MVC, MVP to a better Architecture

  • To overcome the drawbacks of MVC, MVP architecture modifications were done to segregate it to a better representation of architecture called Model-View-ViewModel.
  • It is one of the very popular ways of structuring the android code.
  • MVVM helps you to separate your code into many layers that let you have your own responsibilities.
  • Code becomes more readable, testable, maintainable.
  • The codebase stands the test of time.
Contents
MVVM block diagram
MVVM + Repository Pattern block diagram
Updates to view layer from view-model
Code Summary
Advantages

MVVM block diagram

* `MVVM` handles the interaction between `view` and the `view model` in a specific way. * The `view-model` gethers the data from the `model` and provides to view in a specific way. * The `view` sends the events to the `view-model` and updates the `model`.

MVVM + Repository Pattern block diagram

* The `MVVM` can easily be upgraded with additional pattern such as `repository pattern`. * The repository helps to manage data from `local-databse` and `remote data-store`.

Updates to view layer from view-model

  • We can use live data to update the view from view-model.

Code Summary

Content
What is Live Data
Communicating between view and view-model
Communicating between view-model and the repository

What is Live Data

  • LiveData is an observable data holder class.
  • Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services.
  • This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.

Communicating between view and view-model

  • When we want to call a method in view-model, We call a public function in the view-model that delegates the control to the repository.
  • Once the data is retrieved from the repository, we just update the `live-data in the view model.
  • When you update live data, all the observers of the `live-data are notified.
  • Since the LiveData observer is present in activity/fragment, Control is received in view and we can update the view.

Communicating between view-model and the repository

  • Since the view-model has access to the repository reference we can call the methods in the repository
  • We can have a contract implemented in the repository similar to our MVP representation to communicate between the view-model and repository.
  • Now if we want to send a callback from repository to the view-model just we use the contract, the callback we which can be set in the repository using the init() method in view-model.

Advantages

  • Since we are using LiveData to communicate between view and the view-model
    • Ensures your UI matches your data state
    • No memory leaks
    • No crashes due to stopped activities
    • No more manual lifecycle handling
    • Always up to date data
    • Proper configuration changes
    • Sharing resources
  • Most important advantage of using view-model being it can handle configuration changes, meaning even when activity/fragment is destroyed and recreated the view-model is not destroyed immediately meaning it can retain the date whose references are kept in the view-model.
  • If we use AndroidViewModel instead of ViewModel we can have access to the android context which can be handy sometimes but ideally we should not have android context in view model, It should be available only in the view. We can achieve this using DI-dependency injection for context dependents artifacts in view-model and repository etc.

Clone this wiki locally