我需要多次订阅相同的共享 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;。