RxJava doOnError 和 onErrorReturn 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32348672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does RxJava doOnError and onErrorReturn work?
提问by Nilzor
I made these unit tests, and the outcome is not what I expected at all:
我做了这些单元测试,结果完全不是我所期望的:
// This one outputs "subscribe.onError"
@Test
public void observable_doOnError_subscribingToError() throws InterruptedException {
Observable<String> obs = getErrorProducingObservable();
obs.doOnError(throwable -> System.out.println("doOnError"));
obs.subscribeOn(Schedulers.immediate()).observeOn(Schedulers.immediate()).subscribe(
s -> {},
error -> System.out.println("subscribe.onError")
);
Thread.sleep(300);
}
// This one outputs "subscribe.onError"
@Test
public void observable_onErrorReturn() throws InterruptedException {
Observable<String> obs = getErrorProducingObservable();
obs.onErrorReturn(throwable -> "Yeah I got this");
obs.subscribeOn(Schedulers.immediate()).observeOn(Schedulers.immediate()).subscribe(
s -> System.out.println("got: " + s),
error -> System.out.println("subscribe.onError")
);
Thread.sleep(300);
}
private Observable<String> getErrorProducingObservable() {
return Observable.create(subscriber -> {
subscriber.onError(new RuntimeException("Somebody set up us the bomb"));
});
}
So both output "subscribe.onError" - neither doOnError
nor onErrorReturn
seems to be called.
所以两个输出“subscribe.onError” -似乎也doOnError
没有onErrorReturn
被调用。
doOnError
is documented as:
doOnError
记录为:
Modifies the source Observable so that it invokes an action if it calls onError.
修改源 Observable 使其在调用 onError 时调用操作。
I'm not sure how to intepret that, but I expected either "doOnError" to be output or "doOnError" followed by "subscribe.onError".
我不确定如何解释这一点,但我希望输出“doOnError”或“doOnError”后跟“subscribe.onError”。
onErrorReturn
is documented as :
onErrorReturn
记录为:
Instructs an Observable to emit an item (returned by a specified function) rather than invoking onError if it encounters an error.
指示 Observable 发出一个项目(由指定的函数返回),而不是在遇到错误时调用 onError。
Hence I was expecting "got: Yeah I got this" as output from the latter test.
因此,我期待“得到:是的,我得到了这个”作为后一个测试的输出。
What gives?
是什么赋予了?
采纳答案by marstran
Both doOnError
and onErrorReturn
returns a new Observable
with the changed behaviour. I agree that the documentation of them may be a little misleading. Modify your tests like this to get the expected behaviour:
双方doOnError
并onErrorReturn
返回一个新的Observable
与行为改变。我同意他们的文档可能有点误导。像这样修改您的测试以获得预期的行为:
// This one outputs "subscribe.onError"
@Test
public void observable_doOnError_subscribingToError() throws InterruptedException {
Observable<String> obs =
getErrorProducingObservable()
.doOnError(throwable -> System.out.println("doOnError"));
obs.subscribeOn(Schedulers.immediate()).observeOn(Schedulers.immediate()).subscribe(
s -> {},
error -> System.out.println("subscribe.onError")
);
Thread.sleep(300);
}
// This one outputs "subscribe.onError"
@Test
public void observable_onErrorReturn() throws InterruptedException {
Observable<String> obs =
getErrorProducingObservable()
.onErrorReturn(throwable -> "Yeah I got this");
obs.subscribeOn(Schedulers.immediate()).observeOn(Schedulers.immediate()).subscribe(
s -> System.out.println("got: " + s),
error -> System.out.println("subscribe.onError")
);
Thread.sleep(300);
}
private Observable<String> getErrorProducingObservable() {
return Observable.create(subscriber -> {
subscriber.onError(new RuntimeException("Somebody set up us the bomb"));
});
}