Vas
Vas

Reputation: 2064

RxView.clicks() dies after onError event

Here is a sample Rx chain using RxBindings:

RxView.clicks(refreshIcon)
        .flatMap { Observable.error<Throwable>(IllegalArgumentException()) }
        .subscribe(
                { Timber.d("onNext")},
                { error -> Timber.d("onError") })
        .addTo(disposables)

After clicking my refresh icon once, this chain will not run again as a terminal event took place. I am guessing I was under the wrong impression that the subscription takes place whenever a click event is detected, and that it in fact subscribes whenever that block of code gets executed.

Question is how can I make my chain execute/re-execute on every click, even after it hits a terminal event? Looking for something intuitive.

Upvotes: 1

Views: 937

Answers (2)

Geoffrey Marizy
Geoffrey Marizy

Reputation: 5531

Observable must complete when the first error occur, it's in their contract. In order to have your Observable survive terminal event, you will have to dig in RxJava Error handling operators. retry() seems a good fit in your case:

RxView.clicks(refreshIcon)
        .flatMap { ... }
        .retry()
        .subscribe(...)
        .addTo(disposables)

Upvotes: 2

Idanatz
Idanatz

Reputation: 407

It is part of the Rx contract when an error occurred the stream will receive a onError event and will terminate. Unless you actively handle the error, using for example: onErrorResumeNext()

Upvotes: 1

Related Questions