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

  • 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.

主站蜘蛛池模板: 衡阳县| 兴和县| 德惠市| 沈阳市| 鄂托克前旗| 手机| 临洮县| 大姚县| 双鸭山市| 伊吾县| 南丹县| 昌乐县| 星座| 晋江市| 通渭县| 鄂尔多斯市| 龙川县| 和林格尔县| 巴彦县| 崇信县| 天津市| 潮州市| 盐源县| 万安县| 栖霞市| 商水县| 宣恩县| 县级市| 都匀市| 罗江县| 麦盖提县| 大城县| 法库县| 泰顺县| 利川市| 河间市| 西和县| 罗山县| 吉水县| 阿拉善盟| 松滋市|