[英]“More produced than requested” exception when using `replay` and `autoConnect`
I want to create an observable that emits values from underlying hot observable (starting with -1
before that), only when new value is different than the previous one. 我想创建一个可观察值,仅当新值与先前值不同时才从基础热可观察值(从
-1
开始)发出值。 Additionally I want latest value to be immediately emitted to new subscribers. 另外,我希望将最新价值立即发送给新订户。 I've come up with following code:
我想出了以下代码:
PublishSubject<Integer> hotObservable = PublishSubject.create();
Observable<Integer> observable = hotObservable
.startWith(-1)
.distinctUntilChanged()
.replay(1)
.autoConnect(0);
However this fails after first value (always -1
, regardless of what hotObservable
emits before subscribing to observable
) emitted to new subscriber with java.lang.IllegalStateException: more produced than requested
Interestingly, when I don't automatically connect, but manually subscribe: 然而后第一个值(总是失败
-1
,不管是什么hotObservable
发出订阅之前observable
)发射到新的用户与java.lang.IllegalStateException: more produced than requested
有趣的是,当我不自动连接,手动却订阅:
Observable<Integer> observable = hotObservable
.startWith(-1)
.distinctUntilChanged()
.replay(1)
.autoConnect();
observable.subscribe().unsubscribe();
following subscribers work properly, receiving last value and then updates. 以下订阅者正常工作,接收最后一个值,然后更新。
I can't get replay(1).autoConnect(0)
to work, and I feel like I miss something - why would subscribing and unsubscribing work, while autoConnect(0)
wouldn't? 我无法让
replay(1).autoConnect(0)
正常工作,我感觉好像错过了一些事情-为什么要订阅和取消订阅工作,而autoConnect(0)
却不能呢? What's the proper way to create such observable? 创建这种可观察的正确方法是什么?
Here's test method that fails unless I use autoConnect(); observable.subscribe().unsubscribe()
除非我使用
autoConnect(); observable.subscribe().unsubscribe()
否则此测试方法将失败autoConnect(); observable.subscribe().unsubscribe()
autoConnect(); observable.subscribe().unsubscribe()
: autoConnect(); observable.subscribe().unsubscribe()
:
Observable<Integer> observable = hotObservable
.startWith(-1)
.distinctUntilChanged()
.replay(1)
.autoConnect(); // With (0) it fails
observable.subscribe().unsubscribe(); // Needed if we don't auto connnect
hotObservable.onNext(1);
hotObservable.onNext(2);
hotObservable.onNext(3); // I want this value to be received by new subscriber
TestSubscriber<Integer> subscriber = TestSubscriber.create();
observable.subscribe(subscriber);
subscriber.assertNoErrors();
subscriber.assertValues(3);
I don't get the More produced than requested
error with the code above on RxJava 1.1.3. 在以上RxJava 1.1.3上的代码中,我没有得到
More produced than requested
错误。
The reason the assertion fails is that replay
won't request anything from upstream until any of its Subscribers actually request. 断言失败的原因是,在任何订阅者实际请求之前,
replay
不会向上游请求任何内容。 If the TestSubscriber
is the first to subscribe, it will trigger the startWith to emit -1 and then switch to the PublishSubject which doesn't retain any value thus you don't receive anything else. 如果
TestSubscriber
是第一个订阅的用户,它将触发startWith发出-1,然后切换到PublishSubject,它不保留任何值,因此您什么也不会收到。
I believe what you are looking for is BehaviorSubject
which keeps the very last value and starts with that for new subscribers: 我相信您正在寻找的是
BehaviorSubject
,它保留了最后一个值,并从新订户的值开始:
BehaviorSubject<Integer> hotObservable = BehaviorSubject.create(-1);
Observable<Integer> observable = hotObservable.distinctUntilChanged();
hotObservable.onNext(1);
hotObservable.onNext(2);
hotObservable.onNext(3);
TestSubscriber<Integer> subscriber = TestSubscriber.create();
observable.subscribe(subscriber);
subscriber.assertNoErrors();
subscriber.assertValue(3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.