gpt4 book ai didi

java - 出错后重启 BehaviorSubject

转载 作者:搜寻专家 更新时间:2023-11-01 08:31:51 24 4
gpt4 key购买 nike

通缉行为:

subject = BehaviorSubject.create(1);
subject.subscribe(number -> print(number), error -> print(error));

subject.onNext(2);
subject.onNext(3);
subject.onError(new RuntimeException("I'm an error"));
subject.onNext(4);
subject.onNext(5);

有了这个输出:

1
2
3
I'm an error
4
5

我的问题是 onError 之后的 onNext 不工作(这是有意的,遵循 RxJava 规则),但我会需要对错误有弹性,同时也将错误传递到流中(向用户显示一些反馈)。

有办法吗?

最佳答案

如果您希望忽略 onErroronComplete,可以使用 RxRelay图书馆。

描述:

Subjects are useful to bridge the gap between non-Rx APIs. However, they are stateful in a damaging way: when they receive an onComplete or onError they no longer become usable for moving data. This is the observable contract and sometimes it is the desired behavior. Most times it is not.

Relays are simply Subjects without the aforementioned property. They allow you to bridge non-Rx APIs into Rx easily, and without the worry of accidentally triggering a terminal state.

关于java - 出错后重启 BehaviorSubject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39849600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com