Here is my sample code with the result:
Observable.range(0, 10)
.flatMap(actNumber -> {
if (actNumber == 3) {
return Observable.error(new Throwable("This is an error"));
}
return Observable.just(actNumber);
})
.onErrorReturnItem(-1)
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer number) throws Exception {
Logs.d("number -> " + number);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.d("RX", "Error: " + throwable.toString());
}
});
0, 1, 2, -1
Is it ok, but how can I set it that for this result?
0, 1, 2, -1, 4, 5, 6, 7, 8, 9
Thank you, Robert
According to the Observable contract, once an Observable emits
onError
oronCompleted
, then no more elements will be emitted by the Observable signal. The problem that you are having is that you are returningObservable.error()
if the number is3
which will emit anonError
to yourConsumer
and then terminate the signal.By the contract, you cannot continue a signal after it errors. However what you can do is catch and recover from the error and begin to return a new Observable signal. The documentation for the operator can be found here.
Here is an example
(Note: I did not test this code, but it should point you in the right