When to use Livedata or when to use Rxjava (or Rxjava2 / Rxkotlin)?

Asked

Viewed 161 times

0

After the LiveData was introduced as architecture component of Jetpack, I have seen much discussion about the use of it. The most common is that it is used in the layer viewmodel of MVVM, standard recommended by architectural guide to Android.

I know that the LiveData is prepared to handle the application lifecycle, either from a Activity, one Fragment or a service. And I also know that the resource universe of livedata is much smaller than the RxJava. I also know that the Livedata is synchronous and Rxjava is asynchronous, but this can be solved with the corroding in Kotlin.

Much is said about using the livedata in the layer viewmodel, but I’ve seen several codelabs of Google showing the use of livedata in the repository and also with the Room, for example, see these links:

Using Livedata with Room

Advanced Coroutines with Kotlin Flow and Livedata

So there is some practical reason or situation why I should choose to use Rxjava (or Rxjava2 / Rxkotlin) instead of using Livedata in conjunction with Corrotinas and other components of Jetpack?

2 answers

0

Livedata contains some limitations in relation to Rxjava, for example the lack of thread manipulation, (What is solved in Livedata with the help of coroutines), the big advantage in turn of Livedata is the startup and completion based on the life cycle of Fragment or Activity.


Learning curve

The Rxjava/Rxkotlin requires a longer learning time due to the amount of functionality and to offer several options.

The Livedata after reading some articles and applying in the code, in a short time you will already be using quietly in your projects.

If you are starting I recommend starting with Livedata which will supply most of your needs. Now if you want more control of events in the apps search Rxjava.

The important thing is to understand that they are different technologies and their use depends solely and exclusively on their comfort in using them.

Sources:

https://www.bignerdranch.com/blog/livedatareactivestreams-where-rxjava-meets-livedata/

https://medium.com/@s4y.Solutions/android-livedata-vs-rxjava-b7fd4e166f24

https://stackoverflow.com/questions/51665564/livedata-vs-rxjava

-1

The reason why Voce saw the use of liveData being used together with Viewmodel, is because it is strongly recommended to use a Viewmodel to prepare the data to be displayed in a View, that View itself should not manage anything, only display the data passed from Viewmodel and one of the best ways to make this link is through the use of Livedata, where Viewmodel takes the results obtained from the source (through the Repository for example consuming a web service or a db) and puts in a Livedata. The View that only knows how to display the data observes that Livedata that when updated automatically updates the View value. For this to work more fluidly use of viewBinding allows you to pass the Viewmodel directly to xml and Voce can declare directly in xml which liveData each component must receive the data. (Reactive UI)

In this case also, the part of Viewmodel -> Repository Voce may be using for example Rxkotlin/Rxjava.

https://medium.com/kayvan-kaseb/using-android-livedata-2efe44339c05

Browser other questions tagged

You are not signed in. Login or sign up in order to post.