These are chat archives for ReactiveX/RxJava

15th
Dec 2017
David Whetstone
@humblehacker
Dec 15 2017 02:38
Anyone here had experience with doFinally, doOnTerminate, and/or doAfterTerminate not getting called?
David Whetstone
@humblehacker
Dec 15 2017 02:43
Here’s roughly what I’m doing:
button.clicks()
    .compose(bindToLifecycle())
    .doOnNext { something() }
    .flatMap { somethingReturingAPublishSubject() }
    .doFinally { somethingElse() }
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeBy(
        onNext = { theMainThing() },
        onError = { handleError() })
RxJava2 on Android
David Karnok
@akarnokd
Dec 15 2017 08:40
Those operators require a finite sequence, but I guess clicks() never completes.
David Whetstone
@humblehacker
Dec 15 2017 16:03
Thanks, @akarnokd. That was it. I assumed flatMap sort of replaced the original observable with the result of somethingReturningAPublishSubject(), but I learned that it merges the two—completing only when both the original observables complete.
David Whetstone
@humblehacker
Dec 15 2017 18:25
I see two ways to handle this. First, use take(1) to make clicks() a finite sequence, but this requires setting up the stream again after every click.
fun setupButton() {
    button.clicks()
      .take(1)
      .flatMap { somethingReturningAnObservable() }
      .observeOn(AndroidSchedulers.mainThread())
      .doFinally { 
        something() 
        setupButton()
      }
      .subscribeBy(
          onNext = { theMainThing() },
          onError = { handleError() }
      )
  }
Or, chain the streams:
  fun setupButton2() {
    button.clicks()
      .subscribeBy(
          onNext = { 
            somethingReturningAnObservable()
              .observeOn(AndroidSchedulers.mainThread())
              .doFinally { something() }
              .subscribeBy(
                  onNext = { theMainThing() },
                  onError = { handleError() }
              )
          }
      )
  }
I’m leaning toward the second approach, though in general it complicates error handling. Anyone have any thoughts on these approaches?