Understanding Types Of Observables In RxJava

Understanding Types Of Observables In RxJava

Observable — Operator — Observer

An Observable is like a speaker that emits the 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.

Understanding Types Of Observables In RxJava

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

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 <> Observer

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 <> SingleObserver

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 <> MaybeObserver

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 <> CompletableObserver

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.

That's it for now.

Happy Learning :)

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

Also, Let’s become friends on Twitter, Linkedin, Github, Quora, and Facebook.