Types Of Observables In RxJava

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
Types Of Observables In RxJava

I am Amit Shekhar, a mentor helping developers in getting high-paying tech jobs.

In this blog, we will learn about the different types Of Observables in RxJava.

Before jumping into the types of Observables. Let's see the components of RxJava.

  • Observable
  • Operator
  • Observer

An Observable is like a speaker that emits a value. It does some work and emits some values.

An Operator is like a translator which translates/modifies data from one form to another form.

An Observer gets those values.

Observable->Speaker
Operator->Translator
Observer->Listener

Types of Observables 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

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

fun getDownloadObservable(): Observable<Int> {
    return Observable.create { emitter ->
        // your downloading code
        // start downloading
        if (!emitter.isDisposed) {
            // send progress
            emitter.onNext(10)
        }
        // downloading...
        if (!emitter.isDisposed) {
            // send progress
            emitter.onNext(75)
        }
        // download complete
        if (!emitter.isDisposed) {
            // send progress
            emitter.onNext(100)
            // send onComplete
            emitter.onComplete()
        }
    }
}

Observer for the Observable

fun getObserver(): Observer<Int> {
    return object : Observer<Int> {
        override fun onSubscribe(d: Disposable) {
            println("onSubscribe")
        }

        override fun onNext(progress: Int) {
            println("onNext : $progress")
        }

        override fun onError(e: Throwable) {
            println("onError : ${e.message}")
        }

        override fun onComplete() {
            println("onComplete")
        }
    }
}

Flowable

Flowable comes into picture when there is a case that the Observable is emitting huge numbers of values that 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 the same as normal Observer.

Single

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

Creating a Single Observable

fun getSingleObservable(): Single<String> {
    return Single.create { emitter ->
        // do some task here
        if (!emitter.isDisposed) {
            emitter.onSuccess("Amit")
        }
    }
}

SingleObserver for Single Observable

fun getSingleObserver(): SingleObserver<String> {
    return object : SingleObserver<String> {

        override fun onSubscribe(d: Disposable) {
            println("onSubscribe")
        }

        override fun onSuccess(data: String) {
            println("onSuccess : $data")
        }

        override fun onError(e: Throwable) {
            println("onError : ${e.message}")
        }
    }
}

Maybe

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

Creating a Maybe Observable

fun getMaybeObservable(): Maybe<String> {
    return Maybe.create { emitter ->
        // do some task here
        if (!emitter.isDisposed) {
            emitter.onSuccess("Amit")
        }
    }
}

MaybeObserver for Maybe Observable

fun getMaybeObserver(): MaybeObserver<String> {
    return object : MaybeObserver<String> {

        override fun onSubscribe(d: Disposable) {
            println("onSubscribe")
        }

        override fun onSuccess(data: String) {
            println("onSuccess : $data")
        }

        override fun onError(e: Throwable) {
            println("onError : ${e.message}")
        }

        override fun onComplete() {
            println("onComplete")
        }
    }
}

Completable

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

Creating a Completable Observable

fun getCompletableObservable(): Completable {
    return Completable.create { emitter ->
        // do some task here
        if (!emitter.isDisposed) {
            emitter.onComplete()
        }
    }
}

CompletableObserver for Completable Observable

fun getCompletableObserver(): CompletableObserver {
    return object : CompletableObserver {

        override fun onSubscribe(d: Disposable) {
            println("onSubscribe")
        }

        override fun onError(e: Throwable) {
            println("onError : ${e.message}")
        }

        override fun onComplete() {
            println("onComplete")
        }
    }
}

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

This was all about the types of Observable in RxJava.

You can find the complete project to learn RxJava here.

Show your love by sharing this blog with your fellow developers.

Master Kotlin Coroutines from here: Mastering Kotlin Coroutines

That's it for now.

Thanks

Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.