RxJava, Kotlin Coroutines and LiveData - How to Choose the Proper Tool?

Photo of Paweł Bocheński

Paweł Bocheński

Updated Jan 31, 2023 • 5 min read
pawel-czerwinski-705954-unsplash

Reactive libraries comparison

Reactive is a huge buzzword in Android development lately. With the rise of RxJava, most of the developers are familiar with the concept. But the world moves on and now there are several tools that can make your app reactive. The new developments are LiveData and Kotlin Coroutines. Is RxJava obsolete now? Or should we stay with proven solutions? Let’s find out.

LiveData

LiveData is a reactive solution of delivering events to UI in Android. It is aware of a life cycle of the owner and because of that, it is simple to use. You don’t need to save any subscription-like object to unsubscribe when needed. However, its simplicity carries some drawbacks - it only works on the Main Thread. Another drawback is that there are no operators or ways of combining results from different LiveData streams.

Pros:

  • Simple and easy to use

  • Supported by Google

Cons:

  • Only works on the main thread, so it is not really a replacement of background threading solution.

  • No operators

  • No way of combining results from different LiveData steams.

RxJava

RxJava takes you into the functional programming world by wrapping computation into “Observables”. It does it with all pros and cons of FP. 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 background processing engine ( use futures / promises instead or read below :D ). It might be a good solution if you wrap all your app around it (see this Jake Wharton talk or this MVI presentation

Pros:

  • Functional paradigm

  • A lot of operators

  • A lot of supporting libraries

Cons:

  • Steep learning curve

  • Functional paradigm

  • Might be an overkill for simple apps

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. I recommend watching Venkat Subramaniam video and Roman Elizarov one. How this makes the coroutines a reactive library? In Kotlin coroutines library you can find Channel implementation that works like a reactive stream (and also implements Reactive Stream Specification, so you can mix it with RxJava 2 Publisher).

In Roman’s talk, he encourages to change the way you think about concurrency and think in “structured concurrency” (manage the state that is either mutable or shared). So there are again some paradigms and patterns to learn. In my opinion, coroutines are similar to Go and its goroutines, so if you're familiar with it already it will be easier for you to wrap your head around coroutines.

Pros:

  • Easy to learn

  • Already in Kotlin

  • Can be used as a stream using `Channel`

  • Can be used in multi-platform projects


Cons:

  • Not yet adopted by the community

  • Still, some learning needed

  • Maybe never gonna need if already know RxJava

  • Can be only used in Kotlin

Work manager

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:

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

Summary

As always, the answer to the question “what to use” is “it depends”. 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 really 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. I can’t wait to use it :)

Photo of Paweł Bocheński

More posts by this author

Paweł Bocheński

Paweł is a software developer with almost nine years of experience. He started as an embedded...
Lost with AI?  Get the most important news weekly, straight to your inbox, curated by our CEO  Subscribe to AI'm Informed

Read more on our Blog

Check out the knowledge base collected and distilled by experienced professionals.

We're Netguru

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency.

Let's talk business