SlideShare a Scribd company logo
1 of 20
Android
MVVM
by Syed Asfandyar Zaidi
DevSky Private Limited
Page 02 of 20
01
What is MVVM
04
Difference B/W
MVP & MVVM
02
About MVVM
03
Difference B/W
MVC & MVVM
05
Data Binding
06
What is Repository
LIST OF C ONTENTS
07
What is Live Data
08
Examples
09
Testing MVVM
10
Discussion Session
Page 03 of 20
What Is MVVM?
MVVM architecture facilitates a separation of development of the
graphical user interface with the help of mark-up language or GUI
code. The full form of MVVM is Model–View–View Model. The
view model of MVVM is a value converter that means that it is view
model’s responsibility for exposing the data objects from the Model
in such a way that objects are easily managed and presented.
MVVM stands for Model, View, ViewModel.
Model: This holds the data of the application. It cannot directly talk to the View.
Generally, it’s recommended to expose the data to the View Model through Observables.
View: It represents the UI of the application devoid of any Application Logic. It
observes the View Model.
ViewModel: It acts as a link between the Model and the View. It’s responsible for
transforming the data from the Model. It provides data streams to the View. It also
uses hooks or callbacks to update the View. It’ll ask for the data from the Model..
Page 04 of 20
About MVVM
Page 05 of 20
About MVVM
Page 06 of 20
Difference between
MVC and MVVM
Here, are the important difference between MVVM and MVC
Page 07 of 20
Difference between
MVP and MVVM
Here, are the important difference between MVVM and MVC
Page 08 of 20
WhatisdatabindinginMVVMAndroid?
The Data Binding Library is a support library that allows you to bind
UI components in your layouts to data sources in your app using a
declarative format rather than programmatically. Layouts are often
defined in activities with code that calls UI framework methods.
Page 09 of 20
WhatisdatabindinginMVVMAndroid?
Page 10 of 20
WhatisTwo-WayDataBinding
As the name suggests it’s nothing but a binding view and model
together to get the latest updates both ways.
Page 11 of 20
WhatisTwo-WayDataBinding
Page 12 of 20
WhatisRepositoryinAndroid’s
MVVMarchitecture?
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.
Page 13 of 20
WhatisRepositoryinAndroid’s
MVVMarchitecture?
Page 14 of 20
WhatisRepositoryinAndroid’s
MVVMarchitecture?
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.
Page 15 of 20
WhatisLiveData?
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.
Page 16 of 20
ViewModelCodeExample
Page 17 of 20
MainActivityCodeExample
Page 18 of 20
TestingMVVM
Testing is straightforward when you apply a good pattern. There are a few secrets to
testing MVVM, but here’s the best approaches.
Start testing the ViewModel. Test each one of the public methods, going through as
many cases as you can. Not just the obvious paths, but real edge cases that you’re sure
are never going to happen. Trust me: users and future developers will reach those edge
cases. If something isn’t testable, wrap it! Create an interface that contains all of the
methods you need from the untestable class. Create a class that implements that
interface and instantiate the untestable class you wanted to use inside of it. Then, inside
your class, call the methods from the untestable class. This is called the Proxy design
pattern. Now you have an interface you can use in your app that’s easily replaceable
with a mock object. This way, you can “throw” untestable dependencies outside your
class.
Thank You
Yo u M a y A s k Q u e s t i o n N o w
Discussion Session

More Related Content

Similar to Android MVVM Architecture Explained

MVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,MobileMVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,Mobilenaral
 
Angular JS Basics
Angular JS BasicsAngular JS Basics
Angular JS BasicsMounish Sai
 
VIPER Architecture
VIPER ArchitectureVIPER Architecture
VIPER ArchitectureAhmed Lotfy
 
MVVM presentation
MVVM presentationMVVM presentation
MVVM presentationInova LLC
 
Introduction To MVVM
Introduction To MVVMIntroduction To MVVM
Introduction To MVVMBoulos Dib
 
Mvc, mvp & mvvm (erp)
Mvc, mvp & mvvm (erp)Mvc, mvp & mvvm (erp)
Mvc, mvp & mvvm (erp)Ayesha Maqsood
 
Design patterns in android
Design patterns in androidDesign patterns in android
Design patterns in androidZahra Heydari
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: AndroidJitendra Kumar
 
Code Camp 06 Model View Presenter Architecture
Code Camp 06   Model View Presenter ArchitectureCode Camp 06   Model View Presenter Architecture
Code Camp 06 Model View Presenter Architecturebitburner93
 
Models used in iOS programming, with a focus on MVVM
Models used in iOS programming, with a focus on MVVMModels used in iOS programming, with a focus on MVVM
Models used in iOS programming, with a focus on MVVMAndrei Popa
 
Avigma Tech LLC- Why the MVC pattern so popular?
Avigma Tech LLC- Why the MVC pattern so popular?Avigma Tech LLC- Why the MVC pattern so popular?
Avigma Tech LLC- Why the MVC pattern so popular?Mike Brown
 
Building an enterprise app in silverlight 4 and NHibernate
Building an enterprise app in silverlight 4 and NHibernateBuilding an enterprise app in silverlight 4 and NHibernate
Building an enterprise app in silverlight 4 and NHibernatebwullems
 
Slide Presentation of MVP Pattern Concept
Slide Presentation of MVP Pattern ConceptSlide Presentation of MVP Pattern Concept
Slide Presentation of MVP Pattern ConceptBayu Wijaya Permana Putra
 
Using mvvm inside mvc in domain driven design
Using mvvm inside mvc in domain driven designUsing mvvm inside mvc in domain driven design
Using mvvm inside mvc in domain driven designyashar Aliabasi
 
Ppt of Basic MVC Structure
Ppt of Basic MVC StructurePpt of Basic MVC Structure
Ppt of Basic MVC StructureDipika Wadhvani
 
MVVM Design Pattern NDC2009
MVVM Design Pattern NDC2009MVVM Design Pattern NDC2009
MVVM Design Pattern NDC2009Jonas FollesĂž
 
What Are The Benefits Of Using MVC Framework In ASP.NET Development.pptx
What Are The Benefits Of Using MVC Framework In ASP.NET Development.pptxWhat Are The Benefits Of Using MVC Framework In ASP.NET Development.pptx
What Are The Benefits Of Using MVC Framework In ASP.NET Development.pptxQuickwayInfoSystems3
 

Similar to Android MVVM Architecture Explained (20)

MVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,MobileMVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,Mobile
 
Angular JS Basics
Angular JS BasicsAngular JS Basics
Angular JS Basics
 
VIPER Architecture
VIPER ArchitectureVIPER Architecture
VIPER Architecture
 
MVVM presentation
MVVM presentationMVVM presentation
MVVM presentation
 
Introduction To MVVM
Introduction To MVVMIntroduction To MVVM
Introduction To MVVM
 
Mvc, mvp & mvvm (erp)
Mvc, mvp & mvvm (erp)Mvc, mvp & mvvm (erp)
Mvc, mvp & mvvm (erp)
 
Design patterns in android
Design patterns in androidDesign patterns in android
Design patterns in android
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
 
Spring Framework-II
Spring Framework-IISpring Framework-II
Spring Framework-II
 
Android MVVM
Android MVVMAndroid MVVM
Android MVVM
 
Code Camp 06 Model View Presenter Architecture
Code Camp 06   Model View Presenter ArchitectureCode Camp 06   Model View Presenter Architecture
Code Camp 06 Model View Presenter Architecture
 
Models used in iOS programming, with a focus on MVVM
Models used in iOS programming, with a focus on MVVMModels used in iOS programming, with a focus on MVVM
Models used in iOS programming, with a focus on MVVM
 
Avigma Tech LLC- Why the MVC pattern so popular?
Avigma Tech LLC- Why the MVC pattern so popular?Avigma Tech LLC- Why the MVC pattern so popular?
Avigma Tech LLC- Why the MVC pattern so popular?
 
Building an enterprise app in silverlight 4 and NHibernate
Building an enterprise app in silverlight 4 and NHibernateBuilding an enterprise app in silverlight 4 and NHibernate
Building an enterprise app in silverlight 4 and NHibernate
 
Slide Presentation of MVP Pattern Concept
Slide Presentation of MVP Pattern ConceptSlide Presentation of MVP Pattern Concept
Slide Presentation of MVP Pattern Concept
 
Using mvvm inside mvc in domain driven design
Using mvvm inside mvc in domain driven designUsing mvvm inside mvc in domain driven design
Using mvvm inside mvc in domain driven design
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Ppt of Basic MVC Structure
Ppt of Basic MVC StructurePpt of Basic MVC Structure
Ppt of Basic MVC Structure
 
MVVM Design Pattern NDC2009
MVVM Design Pattern NDC2009MVVM Design Pattern NDC2009
MVVM Design Pattern NDC2009
 
What Are The Benefits Of Using MVC Framework In ASP.NET Development.pptx
What Are The Benefits Of Using MVC Framework In ASP.NET Development.pptxWhat Are The Benefits Of Using MVC Framework In ASP.NET Development.pptx
What Are The Benefits Of Using MVC Framework In ASP.NET Development.pptx
 

Recently uploaded

SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Call Us🔝>àŒ’+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>àŒ’+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>àŒ’+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>àŒ’+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
2.pdf Ejercicios de programaciĂłn competitiva
2.pdf Ejercicios de programaciĂłn competitiva2.pdf Ejercicios de programaciĂłn competitiva
2.pdf Ejercicios de programaciĂłn competitivaDiego IvĂĄn Oliveros Acosta
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
è‹±ć›œUNć­ŠäœèŻ,ćŒ—ćź‰æ™źéĄżć€§ć­ŠæŻ•äžšèŻäčŠ1:1ćˆ¶äœœ
è‹±ć›œUNć­ŠäœèŻ,ćŒ—ćź‰æ™źéĄżć€§ć­ŠæŻ•äžšèŻäčŠ1:1ćˆ¶äœœè‹±ć›œUNć­ŠäœèŻ,ćŒ—ćź‰æ™źéĄżć€§ć­ŠæŻ•äžšèŻäčŠ1:1ćˆ¶äœœ
è‹±ć›œUNć­ŠäœèŻ,ćŒ—ćź‰æ™źéĄżć€§ć­ŠæŻ•äžšèŻäčŠ1:1ćˆ¶äœœqr0udbr0
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesƁukasz Chruƛciel
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 

Recently uploaded (20)

SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Call Us🔝>àŒ’+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>àŒ’+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>àŒ’+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>àŒ’+91-9711147426⇛Call In girls karol bagh (Delhi)
 
2.pdf Ejercicios de programaciĂłn competitiva
2.pdf Ejercicios de programaciĂłn competitiva2.pdf Ejercicios de programaciĂłn competitiva
2.pdf Ejercicios de programaciĂłn competitiva
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
è‹±ć›œUNć­ŠäœèŻ,ćŒ—ćź‰æ™źéĄżć€§ć­ŠæŻ•äžšèŻäčŠ1:1ćˆ¶äœœ
è‹±ć›œUNć­ŠäœèŻ,ćŒ—ćź‰æ™źéĄżć€§ć­ŠæŻ•äžšèŻäčŠ1:1ćˆ¶äœœè‹±ć›œUNć­ŠäœèŻ,ćŒ—ćź‰æ™źéĄżć€§ć­ŠæŻ•äžšèŻäčŠ1:1ćˆ¶äœœ
è‹±ć›œUNć­ŠäœèŻ,ćŒ—ćź‰æ™źéĄżć€§ć­ŠæŻ•äžšèŻäčŠ1:1ćˆ¶äœœ
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 

Android MVVM Architecture Explained

  • 1. Android MVVM by Syed Asfandyar Zaidi DevSky Private Limited
  • 2. Page 02 of 20 01 What is MVVM 04 Difference B/W MVP & MVVM 02 About MVVM 03 Difference B/W MVC & MVVM 05 Data Binding 06 What is Repository LIST OF C ONTENTS 07 What is Live Data 08 Examples 09 Testing MVVM 10 Discussion Session
  • 3. Page 03 of 20 What Is MVVM? MVVM architecture facilitates a separation of development of the graphical user interface with the help of mark-up language or GUI code. The full form of MVVM is Model–View–View Model. The view model of MVVM is a value converter that means that it is view model’s responsibility for exposing the data objects from the Model in such a way that objects are easily managed and presented.
  • 4. MVVM stands for Model, View, ViewModel. Model: This holds the data of the application. It cannot directly talk to the View. Generally, it’s recommended to expose the data to the View Model through Observables. View: It represents the UI of the application devoid of any Application Logic. It observes the View Model. ViewModel: It acts as a link between the Model and the View. It’s responsible for transforming the data from the Model. It provides data streams to the View. It also uses hooks or callbacks to update the View. It’ll ask for the data from the Model.. Page 04 of 20 About MVVM
  • 5. Page 05 of 20 About MVVM
  • 6. Page 06 of 20 Difference between MVC and MVVM Here, are the important difference between MVVM and MVC
  • 7. Page 07 of 20 Difference between MVP and MVVM Here, are the important difference between MVVM and MVC
  • 8. Page 08 of 20 WhatisdatabindinginMVVMAndroid? The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. Layouts are often defined in activities with code that calls UI framework methods.
  • 9. Page 09 of 20 WhatisdatabindinginMVVMAndroid?
  • 10. Page 10 of 20 WhatisTwo-WayDataBinding As the name suggests it’s nothing but a binding view and model together to get the latest updates both ways.
  • 11. Page 11 of 20 WhatisTwo-WayDataBinding
  • 12. Page 12 of 20 WhatisRepositoryinAndroid’s MVVMarchitecture? 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.
  • 13. Page 13 of 20 WhatisRepositoryinAndroid’s MVVMarchitecture?
  • 14. Page 14 of 20 WhatisRepositoryinAndroid’s MVVMarchitecture? 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.
  • 15. Page 15 of 20 WhatisLiveData? 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.
  • 16. Page 16 of 20 ViewModelCodeExample
  • 17. Page 17 of 20 MainActivityCodeExample
  • 18. Page 18 of 20 TestingMVVM Testing is straightforward when you apply a good pattern. There are a few secrets to testing MVVM, but here’s the best approaches. Start testing the ViewModel. Test each one of the public methods, going through as many cases as you can. Not just the obvious paths, but real edge cases that you’re sure are never going to happen. Trust me: users and future developers will reach those edge cases. If something isn’t testable, wrap it! Create an interface that contains all of the methods you need from the untestable class. Create a class that implements that interface and instantiate the untestable class you wanted to use inside of it. Then, inside your class, call the methods from the untestable class. This is called the Proxy design pattern. Now you have an interface you can use in your app that’s easily replaceable with a mock object. This way, you can “throw” untestable dependencies outside your class.
  • 19. Thank You Yo u M a y A s k Q u e s t i o n N o w