Here we go with a lot of new amazing things like CompositeDisposable, MutableLiveData etc. Don’t get panic I will explain it what purpose of these object. First, see in above class that is extended by ViewModel which has some its callback function like onCleared().

ViewModel is part of MVVM design pattern which we have already discussed in starting. But few interesting things about it is that ViewModel class is designed to store and manage UI related data in a lifecycle conscious way. It allows data to survive even in the configuration change.

If the system recreated UI, for example, you rotate your screen then activity get recreated in such case before ViewModel concept you generally used to use onSaveInstanceState() and restore its data by using bundle in onCreate() this process was suitable for small amount of data that you can easily be serialized and deserialized, another problem that supposes you make an asynchronous call that is taking some time to return data then UI controller needs to manage these call and ensure the system cleans them up after it’s destroyed to avoid memory leaks. To handle all these problems you can easily use ViewModel which is lifecycle aware and it will manage this kind of UI related problem.

Now comes to code again here we used CompositeDisposable which is a concept of RxJava.

<strong>disposables</strong>.add(<strong>repository</strong>.executeLogin(mobileNumber, password)<br> .subscribeOn(Schedulers.<em>io</em>())<br> .observeOn(AndroidSchedulers.<em>mainThread</em>())<br> .doOnSubscribe((d) -> <strong>responseLiveData</strong>.setValue(ApiResponse.<em>loading</em>()))<br> .subscribe(<br> result -> <strong>responseLiveData</strong>.setValue(ApiResponse.<em>success</em>(result)),<br> throwable -> <strong>responseLiveData</strong>.setValue(ApiResponse.<em>error</em>(throwable))<br> )); 1 <strong> disposables < / strong > . add ( <strong> repository < / strong > . executeLogin ( mobileNumber , password ) <br> . subscribeOn ( Schedulers . <em> io < / em > ( ) ) <br> . observeOn ( AndroidSchedulers . <em> mainThread < / em > ( ) ) <br> . doOnSubscribe ( ( d ) - & gt ; <strong> responseLiveData < / strong > . setValue ( ApiResponse . <em> loading < / em > ( ) ) ) <br> . subscribe ( <br> result - & gt ; <strong> responseLiveData < / strong > . setValue ( ApiResponse . <em> success < / em > ( result ) ) , <br> throwable - & gt ; <strong> responseLiveData < / strong > . setValue ( ApiResponse . <em> error < / em > ( throwable ) ) <br> ) ) ;

I made REST API call using RxJava instead of any thread or async task. So let’s discuss few things about RxJava.

What is RxJava?

“RxJava is a Java VM implementation of Reactive Extensions. It has become the single most important skill for Android development.”

RxJava works on Observer pattern. It has basically three major component.

1.Observable

2.Subscriber

3.Observer



To understand it in better way go to this “link”.

In our code we used subscribe() this means where we want to subscribe process generally we use IO thread by using Schedulers.io(), the second function is observeOn() which indicate where user wanted to consume data comes from observable and where we want to consume it in our case we use MainThread by using AndroidSchedulers.mainThread().

We use CompositeDisposable to add each subscription and clear this inside onCleared() callback of ViewModel.

Now comes to MutableLiveData, before understanding MutableLiveData first let’s have a look of LiveData.

What is LiveData?

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. For More information go to this “article” or “this”.

The advantages of using LiveData

1 .Ensures your UI matches your data state.

2. No memory leaks.

3. No crashes due to stopped activities.

4. No more manual lifecycle handling.

5. Sharing resources.

LiveData has no publicly available methods to update the stored data. So basically we need MutableLiveData.

The MutableLiveData class exposes the setValue(T)and postValue(T)methods publicly and you must use these if you need to edit the value stored in a MutableLiveData object. Usually, MutableLiveData is used in the ViewModel and then the ViewModel only exposes immutable MutableLiveData objects to the observers. We are setting value in MutableLiveData object like:

<strong>responseLiveData</strong>.setValue(ApiResponse.<em>loading</em>()) 1 <strong> responseLiveData < / strong > . setValue ( ApiResponse . <em> loading < / em > ( ) )

Now its time to move further in code, we used Repository class here you can see the implementation of this class here.