Chapter 5: Disposables and Specific Observables: Single/MayBe/Completable

android2ee (Seguy Mathias)
3 min readDec 2, 2020

We will see that every Observable is a Disposable object when subscribed. It will show us how to stop an Observables when no more observers needs him.

Then we will look at specific Observables :

  • Single
  • Maybe
  • Completable

Disposable

Every time you subscribe to an Observable, a Disposable is returned. So in the context of the Observer you can stop the observable.
For example, let’s consider this Observable:

In the above example, you can see that after 5 seconds, the Observable is stopped by the context of the observer:

Two questions raised from this example.

What happened is two observers are listening to the observers and only one dispose the Observable ?

Only the Observer linked to the dispoable is dispose when dispose is called on it

The output is clear, the second Observers is still running for the next 5s until it is also disposed.

What happened when the Observable is dispose, can we reuse it ?

The answer is yes, we can reuse it.

And the output is:

Conclusion

Disposable is a great feature that help us dispose the resource of the Observable.
The Disposable is linked to the “instance” of the observable. It is returned by calling subscribe on it.
If you dispose one observable, only its instance will be dispose.
If you dispose an observable and then relaunch it, it will restart.
Remark: The observable has a isDisposed method to check if it has to stop.

Single

Single is a specific Observable that will emits only one item and then close by calling onComplete

It has a specific Observer;

  • OnNext method is changed by onSuccess method (because only one emission):
  • OnComplete method is gone:

Maybe

Maybe is a specific Observable that will emits only one item xor the onComplete event

It has a specific Observer;

  • OnNext method is changed by onSuccess method (because only one emission)

Remark: Only one event is send, either success, either complete, either error.
We can see this behavior by using the maybeObserver below in the following code:

The output is obvious:

Completable

Completable is a specific Observable that will emits only onComplete. The goal is to run an operation and prevent observer that it’s done.
You can use FromRunnable to create it. There are a lot of creators for this Observable.

The associated Observer is the following.

It has a specific Observer;

  • OnNext method is gone

More on the topic

Previous chapter: Chapter 4: Observable simple creators

Next chapter: Chapter 6: Observable operators

This chapter is a small part of a biggest training project, you can find on Github. You’ll have the main branch with the answers/responses/unit tests and documentation. You could start with the “questions” branch to only have the questions.

You’ll have the full table of content of the articles on Medium.

You can download the book on Android2ee

Warning: Those articles has been created only because I am stuck in a Rx Android project and I need to prepare the ramp up for those who will replace me.

My conclusion : Choose Kotlin and suspending function instead of Rx. PLEASE, save your project from RxHell !!!

Write Medium in Markdown? Try Markdium!

--

--

android2ee (Seguy Mathias)

Android2ee alias Mathias Seguy. Android expert gives you some news on the Android world. mathias.seguy@android2ee.com