简体   繁体   English

RxJava在多个订户之间共享Observable的排放

[英]RxJava share an Observable's emissions between multiple subscribers

I have the following problem: 我有以下问题:

I have a observable that is doing some work, but other observables need the output of that observable to work. 我有一个可观察的东西正在做一些工作,但是其他的可观察者需要那个observable的输出才能工作。 I have tried to subscribe several times on the same observable, but inside the log i see that the original observable is initiated multiple times. 我试图在同一个observable上多次订阅,但在日志中我看到原始的observable被多次启动。

thats my observable thats create the object: 多数民众赞成我的observable创建对象:

Observable.create((Observable.OnSubscribe<Api>) subscriber -> {
            if (mApi == null) {
                //do some work
            }
            subscriber.onNext(mApi);
            subscriber.unsubscribe();
        })

thats my observable that needs the object 这就是我需要对象的可观察性

loadApi().flatMap(api -> api....()));

I'm using 我正在使用

.subscribeOn(Schedulers.io()) observable.observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()

on all observables. 在所有可观察的。

I'm not sure that I understood your question correctly, but I figure you're looking for a way to share the emissions of an observable between several subscribers. 我不确定我是否正确理解了你的问题,但我认为你正在寻找一种方法来分享几个订阅者之间可观察的排放量。 There are several ways of doing this. 有几种方法可以做到这一点。 For one, you could use a Connectable Observable like so: 首先,您可以像这样使用Connectable Observable

ConnectableObservable<Integer> obs = Observable.range(1,3).publish();
obs.subscribe(item -> System.out.println("Sub A: " + item));
obs.subscribe(item -> System.out.println("Sub B: " + item));
obs.connect(); //Now the source observable starts emitting items

Output: 输出:

Sub A: 1
Sub B: 1
Sub A: 2
Sub B: 2
Sub A: 3
Sub B: 3

Alternatively, you could use a PublishSubject : 或者,您可以使用PublishSubject

PublishSubject<Integer> subject = PublishSubject.create(); //Create a publish subject
subject.subscribe(item -> System.out.println("Sub A: " + item)); //Subscribe both subscribers on the publish subject
subject.subscribe(item -> System.out.println("Sub B: " + item));    
Observable.range(1,3).subscribe(subject); //Subscribe the subject on the source observable

Output: 输出:

Sub A: 1
Sub B: 1
Sub A: 2
Sub B: 2
Sub A: 3
Sub B: 3

Both of these examples are single threaded, but you can easily add observeOn or subscirbeOn calls to make them async. 这两个示例都是单线程的,但您可以轻松添加observeOn或subscirbeOn调用以使它们异步。

First of all using Observable.create is tricky and easy to get wrong. 首先使用Observable.create是棘手的,容易出错。 You need something like 你需要类似的东西

Observable.create(subscriber -> {
        if (mApi == null) {
            //do some work
        }
        if (!subscriber.isUnsubscribed()) {
           subscriber.onNext(mApi);
           subscriber.onCompleted();
           // Not subscriber.unsubscribe(); 
        }            
 })

You can use 您可以使用

ConnectableObservable<Integer> obs = Observable.just(1).replay(1).autoConnect();

All subsequent subscribers should get the single emitted item 所有后续订阅者都应获得单个发出的项目

obs.subscribe(item -> System.out.println("Sub 1 " + item));
obs.subscribe(item -> System.out.println("Sub 2 " + item));
obs.subscribe(item -> System.out.println("Sub 3 " + item));
obs.subscribe(item -> System.out.println("Sub 4 " + item));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM