Understanding Types Of Observables In RxJava

Observable — Operator — Observer

An Observable is like speaker which emit value. It does some work and emits some values.

An Operator is like translator which translate/modify a data from one form to another form.

An Observer gets those values.

Types of Observables in RxJava

This post is all about the different types of Observables available in RxJava.

The following are the different types of Observables in RxJava:

  • Observable
  • Flowable
  • Single
  • Maybe
  • Completable

As there are different types of Observables, there are different types of Observers also.

So, the following are the different types of Observers in RxJava:

  • Observer
  • SingleObserver
  • MaybeObserver
  • CompletableObserver

Now, let’s see how they are different and when to use which one.

Observable <> Observer

This is the simplest Observable which can emit more than one value.

Example use-case: Let’s say you are downloading a file and you have to push the current status of download percentage. Here, you will have to emit more than one value.Creating a simple Observable

Observer for the Observable

Flowable <> Observer

Flowable comes to picture when there is a case that the Observable is emitting huge numbers of values which can’t be consumed by the Observer.

In this case, the Observable needs to skip some values on the basis of some strategy else it will throw an exception.

The Flowable Observable handles the exception with a strategy.

The strategy is called BackPressureStrategy and the exception is called MissingBackPressureException.

Creating a Flowable Observable

Similar to normal Observable, you can create Flowable using Flowable.create().

Observer for Flowable Observable

The Observer for Flowable is exactly same as normal Observer.

Single <> SingleObserver

Single is used when the Observable has to emit only one value like a response from a network call.

Creating a Single Observable

SingleObserver for Single Observable

Maybe <> MaybeObserver

Maybe is used when the Observable has to emit a value or no value.

Creating a Maybe Observable

MaybeObserver for Maybe Observable

Completable <> CompletableObserver

Completable is used when the Observable has to do some task without emitting a value.

Creating a Completable Observable

CompletableObserver for Completable Observable

Now, you can think when to use which Observable depending upon your use-cases.

Complete RxJava Tutorial

RxJava is a Java VM implementation of Reactive Extensions. It has become the single most important skill for Android development.

Learn RxJava.

Happy Learning