I am using Room for my Database management and I was confused in what to use while working with real-time data. For now, to manage real-time data I am using Flowable
and am I pretty satisfied with it. What I was confused is I can use LiveData
as well to do the same operation.
To give some context, here is how I am querying data and updating my view.
addDisposable(userDao().getUsersFlowable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(users -> userAdapter.setUsers(users)));
userDao().getUsersLiveData()
.observe(this, users -> {
userAdapter.setUsers(users)
})
I am not much familiar with LiveData
, but as far as my research goes it is an observer pattern that is also lifecycle aware, meaning that I will stop notifying if UI is not in active state. That said, as you can see in my Flowable
code, I am adding it to CompositeDisposable
and I will dispose in my onDestroy()
method.
So I don't see point of why I should use LiveData
when I can manage everything with RxJava
, which has a lot of operators for convenience.
So when should I use LiveData
and when RxJava
while working with Room. Answers reflecting given scenario is much appreciated, but other use cases are also welcomed.
I followed When to use RxJava in Android and when to use LiveData from Android Architectural Components?, but it's too broad and I couldn't get answer specifically in my case
Normally working with views it's often good to use LiveData. It automatically manages subscription, works really well with DataBinding library. it's sort of a data holder that is lifecycle aware as oppose to stream of data (Rx Concept).
In other cases I would suggest using RxJava which has powerful operator chains for transformation and concurrency. Hope it sheds some lights on your understanding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With