1

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.

Vas
  • 2,014
  • 3
  • 22
  • 38

2 Answers2

2

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)
Geoffrey Marizy
  • 5,282
  • 2
  • 26
  • 29
  • As I suspected. If I use a monad, would the chain not terminate? I'm guessing I simply handle the monad in my subscribe() block? – Vas Mar 12 '18 at 16:06
  • I added the relevant sample for you. I also removed the monad digression, since it was more confusing than helping. Wrapping in a monad will terminate, except if you write some complicate operator on your own. – Geoffrey Marizy Mar 12 '18 at 17:50
  • 1
    That seems to work. So when observing a hot source, retry will resubscribe, but will wait for the next emission. This is great to know. Thank you. – Vas Mar 13 '18 at 11:55
  • Maybe you could accept the answer since the problem is solved. – Geoffrey Marizy Mar 13 '18 at 11:57
1

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()

Idanatz
  • 397
  • 2
  • 8