Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava: what is difference between callbacks in doOnError('callback') and subscribe(*, 'callback')

In my last project, I use rxJava and I realize that observable.doOnError('onErrorCallback').subscribe(action) and observable.subscribe(action, 'onErrorCallback') behave in different ways. Even from docs it's not obvious for me what's exactly the difference between them and when I should use first and second variant.

like image 978
Stepango Avatar asked May 15 '15 02:05

Stepango


1 Answers

The doOnError operator allows you to inject side-effect into the error propagation of a sequence, but does not stop the error propagation itself. The Subscriber is the final destination of the events and they 'exit' the sequence.

You can see the usefulness of doOnError with the following example:

api.getData()
.doOnError(e -> log.error(e))
.retry(2)
.subscribe(...)

It allows you to peek into the error but lets you retry in case of an error. With an end subscriber:

api.getData()
.subscribe(v -> {}, e -> log.error(e) );

You have to arrange the handling of the error (besides the logging) on your own way.

like image 148
akarnokd Avatar answered Sep 18 '22 03:09

akarnokd