Which one to use in Room: LiveData or RxJava?
Asked Answered
C

2

8

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

Cleaning answered 12/11, 2018 at 2:49 Comment(0)
B
7

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.

Blakeblakelee answered 13/11, 2018 at 6:19 Comment(1)
I guess that answers my question. ThanksCleaning
B
0

This thread might be closed but i have a good solution to the problem of using Rxjava with Room for any transformation you want and then transoform to livedata to observe in your views.

Check my solution here: solution

Broomfield answered 26/9, 2019 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.