rx share:避免重复onNext调用多次订阅相同的订阅者

时间:2017-11-28 11:33:04

标签: rx-java reactive

我需要多次订阅相同的共享 Observable

作为多次订阅的副作用,我的Subscriber onNext也会被多次调用(因为内部已添加到SubscriptionList&# 39; s LinkedList又一次。)

是否有一种简单明了的内置方式可以避免针对同一onNext的多个Subscriber来电?

TestScheduler scheduler = Schedulers.test();
TestSubscriber<Long> subscriber = new TestSubscriber<>();
Observable<Long> sharedExec = Observable
        .timer(5, TimeUnit.SECONDS, scheduler)
        .share();


sharedExec.subscribe(subscriber);
sharedExec.subscribe(subscriber);
scheduler.advanceTimeBy(6, TimeUnit.SECONDS);

subscriber.assertValueCount(1);
// java.lang.AssertionError: Number of onNext events differ; expected: 1, actual: 2 (2 completions)

P.S。 rxJavaV =&#39; 1.1.8&#39;。

0 个答案:

没有答案