如何订阅多个订阅者到 Observable 或 Flowable? [英] How to subscribe several subscribers to Observable or Flowable?

查看:39
本文介绍了如何订阅多个订阅者到 Observable 或 Flowable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Hello World 示例中,有一个订阅者

In Hello World example there is one subscriber

   public static void main(String[] args) {
      Flowable.just("Hello world").subscribe(System.out::println);
   }

如何制作两个或更多?

推荐答案

您可以为多个订阅者订阅任何 observable/flowable.只需根据需要多次重复 subscribe 调用即可.

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);
...

hotcold observables 在处理如此多订阅的方式上有所不同.

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

observables/flowables 为每个新订阅者从源重新请求项目.例如,Flowable.fromCallable(c) 将在每次订阅时调用 c.

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 与所有订阅者共享相同的源订阅,即它们不会为每个新订阅者从源请求新项目.新项目会传播给所有当前订阅的订阅者.

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.

这篇关于如何订阅多个订阅者到 Observable 或 Flowable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆