How to subscribe several subscribers to Observable or Flowable?

梦想与她 提交于 2019-12-02 05:35:15

You can subscribe multiple subsctibers to any observable/flowable. Just repeat subscribe call as many times as you need.

Flowable<String> source = Flowable.just("Hello world");
source.subscribe(System.out::println);
source.subscribe(System.out::println);
...

There is difference in hot and cold observables in the way they handle such multiple subscriptions.

Cold observables/flowables re-request items from source for every new subscriber. For example, Flowable.fromCallable(c) will invoke c every time it is subscribed to.

Hot observables/flowables share same source subscription with all subscribers, i.e. they do not request new items from source for every new subscriber. New items get propagated to all currently subscribed subscribers.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!