The need for MVVM in Android: Advance learning topic

success_anil
4 min readJan 29, 2020

--

Late Mr. Steve Jobs rightly said

The only way to do great work is to love what you do.

When we developers try to follow the above saying and imbibe it in our daily routine we often find ourselves in a situation where we are finding the scope of improvement in our own applications.

The topic is for advanced learners who are experienced developing Android application and have released the application, maybe they have released the first version of the application to the Play Store and now they want to improve its code

This blog is not for beginners this is for Advanced Learner’s which now want to improve their existing codebase.

Process

In a typical MVC show image here

Let’s take an example of an Android application that shows a list of Tweets from Twitter for the signed-in user.

This topic is not about sign in with twitter feature so I am not going to explain you how sign in with twitter functionality is created if you want to study about it please visit this link

Coming back to our initial discussion View classes with the help of some classes like utility classes, async task networking library like retrofit brings data for the user timeline

So far everything seems to be good now suppose you want the application to show real-time data of the users timeline-like if someone has deleted their own tweet or posted a tweet on that user’s home timeline it should be shown within few minutes in your application.

To add the above feature to the existing application we have to poll twitter API or the server every 1-minute to fetch updates timeline data. This may get improved if use websocket but they have their own pros and cons.

In a typical MVC pattern, we used to link views and utility classes using interfaces or callbacks. Moreover, we used to provide an instance of activity e or Fragment to the utility class so that whenever data comes back from network our utility class or Asynctask or Service Component can update UI (Activity or Fragment) using the callback mechanism.

Problem

The problem with this scenario is the activity or fragment life cycle is governed by Android OS so because of some XYZ reason Android OS chooses to kill our activity or fragment.

when this happens the reference for activity or fragment that we have stored in utility class becomes null.

Now if our utility class or Asynctask or Service Component, bring some data from the network and tries to update UI using invalid or null instance of the activity, will raise a crash. In fact, a lot of crashes if this type of pattern is implemented in lots of places in code.

Imagine all this happened when your application was in production mode and on customers device. Customers don’t want to use those applications the second time which crashes.

The solution to this problem was checking null before using any view classes or UI Component, which makes our code very hard to read and maintain in the long run.

This is the genuine problem Android developers were facing. Then in 2017 Google came up with the idea of MVVM in Android which works on the basis of observers rather than callback. Before going further I would like to say here that MVVM is not the remedy to every problem but yes it makes your code clean.

So for improving our code, View first linked to the ViewModel classes using an observer to get an update from ViewModel.

On the other side, ViewModel is connected with model classes and the repository to get the updated data.

ViewModel has no information on the activity or fragment or UI component, so your model doesn’t directly updates activity.

Instead, it's the activity or fragment that requests ViewModel for updated data. So in the scenarios where activity or fragment is killed by Android Os the observer that is attached to ViewModel will also be removed automatically. So now even if data comes late, it won’t impact application functionality.

Conclusion

As a result of this, there will be few crashes and customer delight. So now you know what was the need to bring MVVM. This brings us to the end of the blog,

Very soon I will be updating code examples. I don’t do great work, but I love to improve what I have coded in the past.

Thanks for reading.

Happy Coding.

--

--

success_anil

Learning via repetition. Long but sturdy, reliable way to learn.