官术网_书友最值得收藏!

  • Spring 5.0 Projects
  • Nilang Patel
  • 229字
  • 2021-07-02 12:35:04

Cold Observable

When the Observable itself creates the procedure or, say, Observable produces the data stream itself, it is said to be cold Observable. Generally Observable is lazy in nature, meaning it only emits the data when any Observer subscribes to it. Cold Observable always starts a fresh execution for each subscriber. 

In other words, cold Observable emits separate data/event streams for individual Observers. All examples we have seen so far were of a cold Observable type, where we have created a data stream with the just() or create() method. Let's see how cold Observable works for more than one Observer subscribed, with the following example.

public class RxJavaColdObservable {
public static void main(String[] args) {
Observable<String> source =
Observable.just("One","Two","Three","Four","Five");
//first observer
source.filter(data->data.contains("o"))
.subscribe(data -> System.out.println("Observer 1 Received:" + data));
//second observer
source.subscribe(data -> System.out.println("Observer 2 Received:" + data));
}
}

In this code, the data is created by Observable itself so it is called cold Observable.  We have subscribed two different Observers. When you run this code, you will get an output as follows:

Cold Observable provides a separate data stream for each Observer so when we applied the filter for first Observer there is no effect in the second Observer. Also if there are more than one Observer, then Observable will emit the sequence of data to all observers one by one.

主站蜘蛛池模板: 桃源县| 湖州市| 崇礼县| 聂荣县| 临猗县| 辉南县| 红桥区| 海淀区| 施甸县| 沂源县| 孟村| 洛扎县| 都匀市| 祁东县| 兰考县| 宁海县| 沁水县| 若尔盖县| 游戏| 衡南县| 青铜峡市| 遂平县| 澎湖县| 巴彦县| 甘德县| 慈溪市| 怀集县| 卢龙县| 顺平县| 封丘县| 云浮市| 常德市| 遂宁市| 色达县| 治多县| 安乡县| 栖霞市| 长丰县| 龙游县| 新民市| 申扎县|