Chapter 4: Observable simple creators

android2ee (Seguy Mathias)
3 min readDec 2, 2020

We will see a list of methods to create Observables. We already have seen:

  • create()
  • just()
  • fromIterable()

Now we will have a look at others useful creators:

  • range
  • interval

And some more miscellaneous ones:

  • empty
  • never
  • error

We will finished by looking at the Defer creator and its factory.

  • defer

Range

Range let you emit a stream of integer

I will emit the first 11 integer. You can also emit long using rangeLong

Interval

I will let you emit a tick every second or any period you want:

You also have some range to add an initial delay, to use a specific scheduler or use intervalRange for emitting integer or long.
** WARNING when using interval, the subscribe is not blocking the Thread.
It’s a non blocking observable. It will call the method and drop it in the Thread.**

Empty, never and error

In some occasions, you would like to emit either:

  • an empty stream with its onComplete event
  • an empty stream without the onComplete event
  • an error event

The Defer

In the previous chapter, we saw that calling subscribe is “cloning” the Observable and run it again.
What can we do if we want that the cloning is in fact a new instantiation ? for example, the Observable uses internal variable states that has to be updated permanently for each observer.

Let’s take an example, you have an observable that tick every n seconds where n is a class variable:

In another class, we want to use this ticker, but a first observer want a tick every second, and the other one, want a tick every two seconds:

The result is clear, both observers receive the event every second:

What we need is to be able to recreate the Observable when a new observer subscribe on it. To do that we will use a Observable factory that will recreate the Observable every time a new observer subscribe on it:

So now, every times a new observer is subscribing to the ObservableDefered, it will instantiate a new Observable using the factory.
And it will just work:

The result is also clear, the second Observer will receive the event every 2 seconds, while the first will keep receiving them every second.

Conclusion

In some case, you would like to update the Observable every time a new observer is subscribing on it. To do that a simple trick is to use Defer and its factory.
You’ll be able to recreate your observable.

More on the topic

Previous chapter: Chapter 3: What happened when several observers subscribe to the same Observable? Hot Observable Versus Cold Observable

Next chapter:

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