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: Value3As 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: Value3ConnectableObservable 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 observersOutput:
Observer1: Value1
Observer2: Value1
Observer1: Value2
Observer2: Value2
Observer1: Value3
Observer2: Value3





