Livedata vs Rxjava
Asked Answered
O

2

9

I have to spend a lot of time learning and implementing Livedata (especially MediatorLivedata) in business logic because it helps to add data from various sources. Had a success with it, because I could add FCM, Network or RoomDatabase as different sources to my MediatorLivedata and observe it in ViewModel. Its actually scales very well. However, it still lacks powerful RxJava2 implementation, like chaining requests well, flat map and etc. It seems that RxJava is used primarily in Business logic, but in fact, I saw a lot of companies using RxJava with UI with additional features/libraries. This actually makes LiveData irrelevant in presentation logic. So I would like to know if LiveData is somehow better/cleaner in presentation logic(using it in a ViewModel) vs RxJava. What would you suggest looking into the future?

Overmatch answered 3/8, 2018 at 4:55 Comment(1)
F
2

RxJava is neither better nor worse to LiveData, it is different. Android Architecture Components was designed to give a model architectural pattern to android developers. So, if you're comfortable with LiveData, use that, or if you're comfortable with RxJava, use that. You can do all operations using both libraries. Though RxJava do contain a lot of syntactic sugar, same can be achieved using Livedata also.

Faqir answered 5/8, 2018 at 14:48 Comment(1)
Any examples on this? I'd really like to see the ViewModel as I guess it will have both LD and RxJava code.Mimir
M
2

I think you can use RxJava and LiveData altogether. RxJava will use to under Model layer, use it for the request and receive data from the server or from the local database.

Then the LiveData will be registered to the Activity/Fragment because with LiveData you can handle the Activity/Fragment life-circle easily, and it was designed for that purpose.

Militate answered 16/11, 2018 at 7:2 Comment(1)
Do you know a sample app or code snippets showcasing the use of both LiveData and RxJava's Flowable? I'd really like to see the ViewModel as it will combine both LD and RxJava (right?).Mimir

© 2022 - 2024 — McMap. All rights reserved.