zocada

A brief intro to RxKotlin/RxJava on Android

RxJava? What is it?

RxKotlin/RxJava provides an easy and efficient way to handle asynchronous stream of data in programs running on Java Virtual Machine (JVM).

Some examples of asynchronous events can be Network Requests, Touch Input Events etc.

Rx in RxKotlin stands for Reactive Extensions. 

On Android when it comes to handling operations that must be done off the UI thread there are a lot of options like AsyncTask, Loaders, JobDispatcher etc.
and what makes RxJava stand out is its flexibility and power of Rx Operators.

But for a novice Android Developer the terminologies and concepts used in RxJava world can be intimidating.

 

Setting Up:

Add this to the build.gradle (app level) module file.

For RxKotlin:

implementation 'io.reactivex.rxjava2:rxkotlin:x.y.z'

For RxJava:

implementation "io.reactivex.rxjava2:rxjava:2.x.y"

RxAndroid Bindings:

implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'


Here are the three important terms that you would come across in Rx World:

1. Observers/Subscribers
2. Observables/Flowables
3. Operators

Observer/Subscriber:

It is someone that observers or requires the data/event from.
It is someone who subscribes to the observable


memegenerator.es

Observable/Flowable:

Is someone/something that emits/produces a stream(s) data which can be observed by the Observer/Subscriber who subscribes to it.


Observable Person

Observables
Creating simple Observables

Here’s how you would make a simple observable that emits/produces 3 integers:

Here’s how you would create a Flowable from a list:

Operators:

Operations that can be performed on the stream of data or observables themselves.

 

Some Operators on Observables:

Rx Operators
Operators usage and chaining

To learn more on Operators, check out Rx Marbles (link below)

 

Reference Links:

 

Rumaan

Associate Android Developer | Web Developer | UI Designer ?