出错后重启 BehaviorSubject
Restart BehaviorSubject after an error
想要的行为:
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
我的问题是 onNext
在 onError
之后不起作用(这是有意的,遵循 RxJava
规则),但我需要对错误有弹性,同时还将它们传递到流中(向用户显示一些反馈)。
有办法吗?
尝试在订阅前使用 .onErrorReturn()
如果您想忽略 onError
和 onComplete
,可以使用 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.
想要的行为:
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
我的问题是 onNext
在 onError
之后不起作用(这是有意的,遵循 RxJava
规则),但我需要对错误有弹性,同时还将它们传递到流中(向用户显示一些反馈)。
有办法吗?
尝试在订阅前使用 .onErrorReturn()
如果您想忽略 onError
和 onComplete
,可以使用 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.