RxJava — Types of Observables
Understanding RxJava for Android Development is like one of the most important things for an Android Developer.
It helps you in many ways to write clean and concise code.
Today, let’s see the different types of Observables in RxJava.
RxJava basically has three types of components. They are:
Observables, Operators, and Observers
They are defined as follows:
Observable emits some values.
Operator operates(modifies) the emitted value by Observable
Observer receives those values emitted by Observable and modified by Operator
Let’s say you go to watch a movie, right? I mean most of us like to watch movies. So what we do once we go to a movie? We take our popcorn and settle in our reserved seats and wait for the movie to start. Once the movie starts our enjoyment towards it depends on some factors like the context of the movie, the sound quality of the screening etc.,
So let’s rewind what we went through here.
The movie on the screen emits the video, the screening hall operates on the audio, enhances it with the quality experience and we receive the synced audio and video respectively.
So here, the movie is the Observable, screening area is the Operator and we, the audience are the Observers
So let’s discuss the various types of Observables in RxJava.
- Observable
- Flowable
- Single
- Maybe
- Completable
As different genres of movies(Observables) attract different types of people(Observers), similarly, there are different types of Observers for all the types of Observables that are discussed above. Let's see what are they
- Observer(Works for Observable and Flowable)
- Single Observer
- Maybe Observer
- Completable Observer
So, let’s understand how these are different from one another.
Observable<>Observer
The Observable that emits more than one value.
Use case:
If the user wants to download a file from the internet, he should be provided with the progress of the upload. In this case, the Observable has to emit values at regular intervals. In this case, we can use this Observable
Flowable<>Observer
Flowable is similar to Observable but this comes into picture when Observable is emitting a huge number of values that can’t be received/consumed by the Observer.
In this case, the Observable needs to skip some values on the basis of strategy or else it will throw an exception.
Here, the flowable Observable makes sense because it handles this exception with a strategy. This strategy is called BackPressureStrategy and this exception is called MissingBackPressureException
A Flowable can be created similar to Observable like Flowable.create()
An Observer for Flowable is same as the observer for Observable
Single<>SingleObserver
Single is used when the Observable has to emit only one value like a response from network call. This is the most common Observable we will be using in RxJava as most of our applications involve Network Calls.
Maybe<>MaybeObserver
Maybe is used when the observable has to emit a value or no value. It is not recommended much to use Maybe in RxJava for Android Application Development
Completable<>CompletableObserver
Completable is used when the Observable has to do some task without emitting a value
Based on the use case of your application, you can select the respective Observable.
RxJava is just awesome.
Thank you for taking the time to read this article.
References: