I am not getting the reason to use RxJava in Android and LiveData from Android Architectural Components.It would be really helpful if the usecases and differences between th
Android LiveData is a variant of the original observer pattern, with the addition of active/inactive transitions. As such, it is very restrictive in its scope.
Using the example described in Android LiveData, a class is created to monitor location data, and register and unregister based on application state.
RxJava provides operators that are much more generalized. Let's assume that this observable will provide location data:
Observable<LocationData> locationObservable;
The implementation of the observable can be built up using Observable.create()
to map the call back operations. When the observable is subscribed, the call back is registered, and when it is unsubscribed, the call back is unregistered. The implementation looks very similar to the code provided in the example.
Let's also assume that you have an observable that emits true when the application is active:
Observable<Boolean> isActive;
Then you can provide all the functionality of LiveData by the following
Observable<LocationData> liveLocation =
isActive
.switchMap( active -> active ? locationObservable : Observable.never() );
The switchMap()
operator will either provide the current location as a stream, or nothing if the application is not active. Once you have the liveLocation
observable, there a lot of things you can do with it using RxJava operators. My favorite example is:
liveLocation.distinctUntilChanged()
.filter( location -> isLocationInAreaOfInterest( location ) )
.subscribe( location -> doSomethingWithNewLocation( location ) );
That will only perform the action when the location changed, and the location is interesting. You can create similar operations that combine time operators to determine speed. More importantly, you can provide detailed control of whether operations happen in the main thread, or a background thread, or a multiple threads, using RxJava operators.
The point of RxJava is that it combines control and timing into a single universe, using operations provided from the library, or even custom operations that you provide.
LiveData addresses only one small part of that universe, the equivalent of building the liveLocation
.
LiveData partially equals to Rx Subject or SharedRxObservable
LiveData manages lifecycle of subscription but Rx Subject subscription should be created and disposed manually
LiveData doesn't have termination state but Rx Subject has OnError and OnCompleted
In fact, LiveData
is not an essentially different tool to RxJava
, so why was it introduced as an architecture component when RxJava
could have easily managed the lifecycle by storing all the subscriptions to observables in a CompositeDispoable
object and then disposing them in onDestroy()
of the Activity
or onDestroyView()
of the Fragment
using only one line of code?
I have answered to this question fully by building a movie search app once using RxJava and then using LiveData here.
But in short, yes, it could, but that would need first overriding the relevant lifecycle methods besides having the basic lifecycle knowledge. This still might not make sense for some, but the fact is that according to one of the Jetpack sessions in Google I/O 2018 many developers find lifecycle management complex. The crash errors arising from not handling lifecycle dependence might be another sign that some developers, even if knowledgable of lifecycle, forget to take care of that in every Activity / Fragment they use in their app. In large apps this could become an issue, notwithstanding the negative effect it could have on productivity.
The bottom line is that by introducing LiveData
, larger number of developers are expected to adopt MVVM without even having to understand the lifecycle management, memory leak and crash. Even though I have no doubt that LiveData
is not comparable with RxJava
in terms of capabilities and the power it gives to developers, reactive programming and RxJava
is a hard-to-understand concept and tool for many. On the other side, I do not think LiveData
is meant to be a replacement for RxJava
–it simply cannot–but a very simple tool for handling a controversial widespread issue experienced by many developers.
** UPDATE ** I have added a new article here where I have explained how misusing LiveData can lead to unexpected results. RxJava can come to rescue in these situations