RxJava VS Coroutines: Which One Should You Pick? (Part I)
Are you an Android developer? Then, let’s get you introduced to a theoretical analysis on RxJava and Coroutines:
The Android world took a turn in 2017 with the announcement of Kotlin as a supported and official Android development language at Google I/O. And then, it happened again with the release of a stable version of Kotlin Coroutines which propelled a big move from RxJava to Coroutines as Android became a Kotlin-first framework.
Android, being based in Java, already presents several tools to help write asynchronous code, but the majority of them do not solve all the issues a programmer may come across.
To go deeper into this conversation, we have to be in touch with some basic concepts, such as what is Asynchronous and Reactive Programming.
Asynchronous programming allows the programmer to instruct code to execute multiple events or instructions, i.e. processing at the same time. Basically, when an asynchronous action starts, another part of the program may continue running. When the action finishes, the program may be signaled with the results.
Reactive programming is a paradigm to code oriented around data flows and the propagation of state change. Given this, it should be possible to express static or dynamic data flows with ease in the programming languages used, and the execution model will propagate change through the data flow. In other words, data flows emitted by one event propagating those changes to the other component that may be registered to receive the data.
These two libraries, RxJava and Kotlin Coroutines, simplify the writing of asynchronous programs.
Rx is a complex library with a wide range of functionality beyond asynchronism, and it targets functional programming instead of imperative, which can be implemented in most programming languages without dedicated support – it works well when the problem in question breaks down easily into a sequence of standard operators. RxJava is a very common tool used for asynchronous calling in Android development and provides a great alternative to typical asynchronous development.
With the rise of RxJava, most developers became familiar with the Reactive concept, but Kotlin turned out to be very successful in the Android world release and is, currently, one of the most popular languages. Recently, it introduced a feature named Coroutines that can make your projects reactive without Rx. Coroutines are quickly approaching stability and have been given focus in many Android and multi-platform – they provide the capability to write concise, asynchronous code and are specifically available in Kotlin for Android.
Programmers with previous experience in mobile development will wonder if it makes sense to replace their Rx know-how and implementations with Coroutines. RxJava and Coroutines present two different approaches to multithreading, sharing similarities but not every goal: RxJava is a library for asynchronous programming that follows the reactive paradigms, while Coroutines purposes is to simplify asynchronous and concurrent programming, not specifically forcing reactiveness.
Next week, we’ll take a closer look to RxJava and Coroutines, their advantages, disadvantages and draft some conclusions. Stay tuned!
Check our blog for more posts like the one you just read!
Comments are closed