avatarIsmail Khan

Summary

The provided content discusses the concepts of Cold, Hot, and Connectable Observables in RxJava2, explaining their behaviors and how they emit data to subscribers.

Abstract

In RxJava2, Cold Observables are defined as those that emit a complete set of items each time a new observer subscribes, ensuring that every observer receives the entire sequence of emissions. This is akin to playing a recorded song where each playback starts from the beginning. In contrast, Hot Observables emit items in real-time, meaning late-subscribing observers may miss earlier emissions, similar to listening to a live radio broadcast. The content also introduces ConnectableObservables, which are Cold Observables that can be converted to act like Hot Observables by using the publish() method followed by a connect() call to initiate emissions to all subscribed observers simultaneously. The article uses code examples to illustrate these concepts, including how to use RxBinding's RxTextView component to observe and react to text changes in an Android application's EditText in real-time.

Opinions

  • Cold Observables are compared to recorded music, emphasizing their predictable and consistent behavior for each subscriber.
  • Hot Observables are likened to live events, highlighting the transient nature of their emissions and the potential for subscribers to miss parts of the data stream.
  • The use of ConnectableObservables is presented as a simple and effective way to control the emission of items to multiple observers, transforming a Cold Observable into a Hot Observable at the programmer's discretion.
  • The article suggests that understanding the distinction between Cold and Hot Observables is crucial for effectively using RxJava2 in real-world applications, particularly those involving event handling or asynchronous data streams.
Photo by Dave Michuda on Unsplash

Cold, Hot & Connectable Observables | RxJava2

Cold Observables are those which emits all the items whenever we subscribe to them. Meaning, if two observers are subscribed to the same observable at different time, they will receive all the emissions from that observable. Its like playing a recorded mp3 song from a device, no matter when we play (subscribe) it we can hear the entire song all the time.

Example

Observable<String> source = Observable.just("Value1", "Value2", "Value3");
source.subscribe(s -> System.out.println("Observer1: "+s));
source.subscribe(s -> System.out.println("Observer2: "+s));

Output

Observer1: Value1
Observer1: Value2
Observer1: Value3
Observer2: Value1
Observer2: Value2
Observer2: Value3

As you can see from the above output both the observers receive all the items (Value1, Value2, Value3) emitted from the observable.

Hot observables are those whichemit items/events at real time. Meaning, if two observers are subscribed to the same observable at different time, the observer which subscribes later will miss the events which were emitted to the observer that subscribed earlier. Its more like listening to a song on a radio, if you tune in late you will miss a part of the song.

Hot observables mostly represents events rather than finite dataset.

Lets make use of RxTextView component of RxBinding library for Android.

RxTextView.textChanges(editText)
                .subscribe(
                           charSequence ->
                               textView.append(charSequence)
                         );

In the above example, we are observing the changes to the EditText and as each character is added it is emitted to the observer which appends the character to a TextView. If you type in ‘a’ ‘b’ ‘c’ in the EditText you can see those characters in the TextView. Let say if another observer subscribe to it now, and the user then types in ‘d’ ‘e’ ‘f’, it won’t receive those previously entered characters, ‘a’ ‘b’ ‘c’, it will only receive the characters ‘d’ ‘e’ ‘f’.

ConnectableObservable helps us to convert a cold observable to hot observable. Its actually very simple, all you need to do is call publish() on any observable and it will return a ConnectableObservable. Once all the observers subscribe to it we then need to call connect() on the returned ConnectableObservable to start the emissions.

Cold Observable Example:

Observable<String> source = Observable.just("Value1", "Value2", "Value3");
//observer1 subscribing
source.subscribe(s -> System.out.println("Observer1: "+s));
//Observer2 subscribing
source.subscribe(s -> System.out.println("Observer2: "+s));

Output:

Observer1: Value1
Observer1: Value2
Observer1: Value3
Observer2: Value1
Observer2: Value2
Observer2: Value3

ConnectableObservable Example:

ConnectableObservable<String> coSource= 
              Observable.just("Value1", "Value2", "Value3")
              .publish();//publish returns ConnectableObservable
//observer1 subscribing
coSource.subscribe(s -> System.out.println("Observer1: "+s));
//Observer2 subscribing
coSource.subscribe(s -> System.out.println("Observer2: "+s));
//calling connect
coSource.connect(); //starts emitting items to observers

Output:

Observer1: Value1
Observer2: Value1
Observer1: Value2
Observer2: Value2
Observer1: Value3
Observer2: Value3
Rxjava
Rxjava2
Rxjava Android
Tutorial Rxjava
Android
Recommended from ReadMedium