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

  • cold observable are emitting data
  • hot observable are emitting event

2 Observers subscribe() to a cold Observable in the same Thread

This will show that:

  • the same set of data is always emitted to each observers.
  • the observers will runs sequentially (first one has to finish before the second one starts) So the code is the following to test this behavior:

Conclusion

The conclusion are the following:
The subscribe() is blocking in onNext and loop in it until onComplete is called or onError.
The subscribe() is creating a new instance of the observables and rerun it.

2 Observers subscribe() to a hot Observable in the same Thread

For starting to understand what happens, let’s just make it simple: subscribe twice to the hot observable and see what happens.

Conclusion

The first remark is that both observers run sequentially and they don’t receive the same set of data.
The second remark is that hot or cold is a vision of the spirit, because in both cases, the important fact to have in mind, is that subscribe is cloning the Observable and run again its creation method.
The conclusion is the same:
The subscribe() is blocking in onNext and loop in it until onComplete is called or onError.
The subscribe() is creating a new instance of the observables and rerun it. So if it emits events or random events, none observers will receives the same data set.

3 Observers subscribe() to a hot Observable in the different Threads

For starting to understand what happens, let’s just make it simple: subscribe twice to the hot observable and run the Observable in another thread and see what happens.
To switch Thread we will use subscribeOn, we will see in another chapter.

hotObserable[Thread]=RxComputationThreadPool-2value is 925
Second Observer value is 925[Thread] RxComputationThreadPool-2
hotObserable[Thread]=RxComputationThreadPool-1value is 957
First Observer value is 957[Thread] RxComputationThreadPool-1
hotObserable[Thread]=RxComputationThreadPool-2value is 875
Second Observer value is 875[Thread] RxComputationThreadPool-2
hotObserable[Thread]=RxComputationThreadPool-1value is 995
First Observer value is 995[Thread] RxComputationThreadPool-1
hotObserable[Thread]=RxComputationThreadPool-2value is 992
Second Observer value is 992[Thread] RxComputationThreadPool-2
hotObserable[Thread]=RxComputationThreadPool-2value is 911
Second Observer value is 911[Thread] RxComputationThreadPool-2
hotObserable[Thread]=RxComputationThreadPool-1value is 896
First Observer value is 896[Thread] RxComputationThreadPool-1

Conclusion

Two remarks:

  • Both observers receieved different events
  • Both observers receive events in random order, it’s not alternating first/second.

ConnectableObservable is the solution to receive from Hot observer the same events

ConnectableObserver is a way to change a “hot observables to a cold one”.
For real, it will wait all the observers to subscribed to it before starting the emission of events. That way all the observers will receive the same set of data.

Conclusion

Only one instance of the Observable is created.
The onNext method runs the one after the other, in a sequential non blocking way.
Calling connect() is blocking the Thread until the emission is over.

More on the topic

Previous chapter: Chapter 2: Observer

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
android2ee (Seguy Mathias)

android2ee (Seguy Mathias)

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