Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one to use in Room: LiveData or RxJava?

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.

Flowable

addDisposable(userDao().getUsersFlowable()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(users -> userAdapter.setUsers(users)));

LiveData

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

like image 682
musooff Avatar asked Nov 12 '18 02:11

musooff


1 Answers

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.

like image 155
Samuel Robert Avatar answered Nov 14 '22 18:11

Samuel Robert