LiveData or RxJava or Coroutines: Android

Sanjay K
3 min readMar 14, 2020

Reactive is a massive buzzword in Android development lately. With the rise of RxJava, most of the developers are familiar with the concept. The new developments are LiveData and Kotlin Coroutines. Is RxJava obsolete now? Or should we stay with proven solutions? So let us get in detail and discuss the pros and cons of each of the solutions mentioned above.

LiveData

LiveData is a reactive solution to delivering events to UI in Android. It is aware of the life cycle of the owner, and because of that, it is simple to use.

You don’t need to save any subscription-like object from unsubscribing when required.

However, its simplicity carries some drawbacks — it only works on the Main Thread. Another disadvantage is that there are no operators or ways of combining results from different LiveData streams.

Pros :

  1. Supported by Google.
  2. Simple and easy to use.

Cons :

  1. As discussed earlier, LiveData only works on the main thread, so it is not a replacement for background threading solution.
  2. It does not provide any operators as well.
  3. There is no way to combine the results from two different LiveData streams.

RxJava

RxJava, developed by Netflix, takes you into the functional programming world by wrapping computation into “Observables.”

On the one hand, you don’t have to deal with the mutable state; on the other, it comes with the whole baggage of functional concepts that you need to get familiar with.

It might be an overkill to use it only as a background processing engine ( use futures/promises instead). It might be the right solution if you wrap all your apps around it.

Pros :

  1. Functional paradigm.
  2. It provides a lot of operators to work with.
  3. It provides a lot of supporting libraries as well.

Cons :

  1. It might be an overkill for simple apps.
  2. Steep learning curve.

Coroutines

Coroutines are a new kid on the block of writing scalable, asynchronous code. Developed by JetBrains are tightly coupled with Kotlin language and available only with it.

The idea behind it is to run jobs in lightweight threads called coroutines but in a simple way.

Kotlin compiler automatically generates state machines from your code, so your code looks like it is normal synchronous code.

Pros :

  1. Easy to learn
  2. It can be used as a stream using `Channel.`
  3. Already in Kotlin
  4. Can be used in multi-platform projects

Cons :

  1. It can be used only in Kotlin.
  2. Some learning is still required.
  3. Not yet adopted by the community.

WorkManager

So maybe you don’t like RxJava, and you don’t want to learn coroutines. Is there anything else you can do? You don’t have to go back and use Event Buses and threads all again.

Google developed this new mechanism called Work Manager. I think it is very cool. It can run the code in your app’s process not only if your app is in the foreground, but also it can take it out of the process and run independently. This way, it is safe to use for long-running operations. How’s this reactive? Work Manager jobs can be observed with LiveData!

Just use, the below functionality:

WorkManager.getInstance().getStatusById(id).observe()

Conclusion

It depends on the scale-type of the project you are building. I would recommend using LiveData + WorkManager combo for small apps, where business logic is not very complicated.

If you already know the RxJava, there is no reason to depreciate it. If you don’t, learning it will, for sure, make you a better developer by exposing to functional programming patterns.

Is there a place for coroutines there? Since it is quite new, I don’t expect everyone to jump on it immediately. I think it will shine in multi-platform projects.

This article is written in a comfortable and short explanation (you can take a look at the complete article written by Paweł Bocheński, the link is provided below). If you find any problem, then please let me know in the comments, and also, if you liked the article, then don’t forget to give it the thumbs up. Cheers!!!

--

--