Android Reactive Programming with ReactiveX — Concept of RxJava, RxKotlin, RxAndroid

Laurence Liu
4 min readDec 6, 2018

What is RxJava and RxKotlin?

It may uses two words to describe these libraries briefly:
OBSERVER PATTERN

RxJava

ReactiveX official GitHub gives this description to RxJava:
Reactive Extensions for the JVM — a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
Simply speaking, RxJava provides not only a large number of amazing operators; such as filter, map, combine, etc, but also offers lot more that can be applied to data stream.

RxKotlin

And they describe RxKotlin as below:
RxKotlin is a lightweight library that adds convenient extension functions to RxJava.
We could interpret this as RxKotlin is specific for Kotlin with some additional classes base on RxJava. View the source code menu from their GitHub and we will know it.

As we know, Kotlin is compatible with Java. Furthermore, Google announced Kotlin as the official language of Android at Google I/O 2017. It is hard to predict when will Kotlin replace Java comprehensively. But it is a good choice to learn new stuff.

When it comes to code Android Apps in Reactive Programming, you have to use RxJava if you are coding in Java project. On the other hand, you could use both RxJava and RxKotlin if you are coding in Kotlin Project.

What is RxAndroid?

Same as RxKotlin, RxAndroid adds the minimum classes to RxJava that make writing reactive components in Android applications easy and hassle-free.

The most important thing of RxAndroid is that it offers Schedulers to manage the thread. Especially Schedulers.io() and AndroidSchedulers.mainThread() are widely used in android programming.

Observable, Observer, Subscribe, Operators, Schedulers

Observable: Observable is a data stream that been observed by Observer and would emit data to Observer.

Observer: Observer is the other side of Observable. It receives the data emitted by Observable.

Subscribe: The bridge between Observable and Observe. There can be multiple Observers subscribed to a single Observable.

Operators: Operators modifies the data which is emit by Observable. The Observer will get the data which has changed.

Schedulers: Schedulers determines which thread should Observable emit the data and on which Observer should receives the data.

The relation between Observable and Observer.

I believe that may puzzled someone who want to start with ReactiveX. So here is an another example which is close to our daily life.

The relation between kids and teacher.
  1. Imaging that the developer who is using ReactiveX is school system.
  2. Teacher has to look after the kids in the school. So the kids are “Observable” and the teacher is “Observer”, which means the kids been observed by the teacher all the time.
  3. They create the relationship by an action called “Subscribe”. When they accomplish this operation, school system gets a message onSubscribe(d).
  4. Also, there is a rule called “Schedulers” which demands the teacher only could observes the kids while they in the classroom. Also the rule requires kids could ignore the teacher when they leave the classroom.
  5. One day, the school system orders the teacher picks the kids who are after kid “E” and report them to the school system. We could comprehend this order as “Operators”.
  6. Those kids who are after “E” is “F, G, H, I”. They report to teacher one by one. Once a kid reported, the school system get a message “onNext(T)”.
  7. When all kids finish the report action, school system will receive a message “onComplete()”.
  8. If there is any mistake happened cause the report action stop; such as the kids run away or the teacher wants to go to bathroom, etc. And school system will receive “onError(e)” message.

Now we have base knowledge about Reactive Programming, ReactiveX, RxJava, RxKotlin and RxAndroid.

Let’s start coding in the next part.

--

--