简体   繁体   English

使用`replay`和`autoConnect`时出现“超出要求的产量”异常

[英]“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.

相关问题 Kafka存储的消息多于产生的消息 - Kafka Storing more messages than Produced 创建的线程数超出了要求 - Creating more threads than requested RxJava异步缓存:处置replay()。autoConnect()的正确方法 - RxJava async cache: proper way to dispose replay().autoConnect() Observable Spring-Data-Jpa-使用多个命名参数时,空指针异常 - Spring-Data-Jpa - Null pointer exception when using more than one named parameters 程序创建的线程比请求的多 - Program creates more threads than requested 尽管具有正确的 DTO,但无法使用请求的结果类型为具有多个返回的查询创建 TypedQuery - Cannot create TypedQuery for query with more than one return using requested result type, despite having correct DTO 使用重播(selectorFoo)但不发布(selectorFoo)时的OOM - OOM when using replay(selectorFoo) but not publish(selectorFoo) 无法使用请求的结果类型为返回多个以上查询的查询创建TypedQuery - Cannot create TypedQuery for query with more than one return using requested result type 具有多个内部异常并使用流的异常 - Exceptions with more than one inner exception and using streams 使用多于硬件线程的注意事项? - Considerations when using more threads than hardware threads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM