Introduction to RxJava2 & RxAndroid

In this ever changing dynamic world of coding, you might have come across the terms RxJava and RxAndroid and might have wondered do I really need to learn these? If the answer was Big No or a little hesitant, do not worry, you have a partner in crime 🙂

However, no matter how much you try to keep yourself away from it, you eventually have to learn this beautiful framework. Just like other frameworks, it might feel like an alien world in the beginning but once you enter it, you will never feel like leaving it. That’s the power of RxJava and RxAndroid. Let’s, deep dive into this more.

What is RxJava?

RxJava is the java implementation of the Reactive Extensions. Reactive Extension is a library which is used for composing asynchronous calls and event-based programs by using observable sequences.

You might have noticed some heavy words in the above statement like Asynchronous calls, EventBased and Observable sequences. Let’s understand each term one by one:

  • Asynchronous: Different part of the program run simultaneously.
  • EventBased: Execution of code depending on the condition which is satisfied. Forex, when a button has clicked an event is triggered which is received by the event handler and some action is performed.
  • Observable: The building block of RxJava is Observables and Subscribers. Observables are used for emitting items and Subscribers are used for consuming those items.

Now, we can sum-up that RxJava is a programming concept for handling asynchronous data streams in a non-blocking manner. Data Stream can be anything from button click to network call to properties to data structures. For instance, Your Facebook feed can be a data stream just like a button click is. One can listen to this stream and react accordingly.

A Stream is nothing but a sequence of ongoing events ordered in time. Let’s understand this by the following example:

We can see the stream can emit three different things, some value of some type, an error or a completed signal. In RxJava, we capture these emitted events asynchronously by defining a function that will execute when a value is emitted or an error is emitted or a completed signal is emitted. This listening of the stream is called subscribing.

Rx java is all about Subscribers and Observables. Subscribers subscribe to the Observables. Observables call Subscribers onNext() for all the number of items it has in the resultset. If anything goes wrong it calls the onError() or subscriber onCompleted().

What are Observables in RxJava?

This sounds more similar to Observer Pattern in Java. However, it is a little smarter than that. For instance, if no Observer is subscribed to Observable no data will be emitted by the Observable which result in better performance. Observables are Instances of the observable class. They observe data Streams and emit them to subscribed Observers.

Observables can be classified into different types namely:

  • Flowable
  • Observable
  • Single
  • Completable

I will share more information on these in the coming blog posts.

What are Observers in RxJava?

They are instances of observer interface and consume the data emitted by the Observables. One Observable can have multiple Observers. Following are the Observer methods:

  • onNext(): Each time Observable emits data this method is called with the resultant data set.
  • onError(): If any error is encountered this method is called.
  • OnCompleted(): This method is called when data emission is over.

What are Operators in RxJava?

In RxJava we have a concept called Operators. The data emitted by the Observables are manipulated by Operators. They tell Observable how to modify the data and when to emit the data. Operators can perform the following operation on the data:

  • Modify
  • Merge
  • Filter
  • Group

Following are the different operators found in RxJava, we will discuss them in detail in coming blog posts:

  • Just
  • From
  • Range
  • Buffer
  • Debounce
  • Filter
  • Repeat
  • Skip
  • Take
  • TakeLast
  • Distinct
  • Count
  • Reduce
  • Max
  • Min
  • Sum
  • Average
  • Concat
  • Merge
  • Map
  • Flat Map
  • Concat Map
  • Switch Map

Schedulers in RxJava

You might think what is the use of RxAndroid? Well, RxAndroid adds classes to RxJava that make writing reactive components in Android easy. Schedulers determine on which thread the work should be done. For Example, it provides a Scheduler that schedules a job on the main thread. So, RxAndroid is just a layer on top of RxJava that provide android specific support.

Following are the different types of Schedulers:

  • IO: Used for IO stuff such as network related stuff, file system operations, etc.
  • Computation: It uses the thread pool mechanism which is dependent on the number of cores present in the system. It is good for performing small operations.
  • New Thread: As the name suggests, it creates a new thread for each active observable.
  • Single: It is always backed by one single thread.
  • Trampoline: It runs the code on the main thread. So if the code is running on the main thread it will add the block of code to the existing main thread.
  • Executor Scheduler: It is provided by the rxAndroid Library and is used to bring back the execution to the main thread so that UI modification can be made.

In the next Blog post, we will get our hands dirty with the code and talk more about some of the operators in RxJava, till then stay tuned…

1 thought on “Introduction to RxJava2 & RxAndroid”

Leave a Reply

Your email address will not be published. Required fields are marked *