Nokuap
Nokuap

Reputation: 2379

Why RxJava Observable.doOnDispose could trigger twice?

This code

@RunWith(JUnit4::class)
class Playground {

    @Test
    fun test() {
        val subject1 = BehaviorSubject.create<Int>()
        val subject2 = BehaviorSubject.create<Int>()
        val observable = subject1.doOnDispose { println("subject1 observable disposed") }
        val disposable = Observable.combineLatest(
            observable.takeUntil { it < 1 },
            subject2.doOnDispose { println("subject2 observable disposed") },
            BiFunction { t1: Int, t2: Int ->
                println("$t1 $t2")
            })
            .subscribe()
        subject1.onNext(1)
        subject2.onNext(0)
        subject1.onNext(0)
        Thread.sleep(100)
        disposable.dispose()
    }
}

Have such output

1 0
0 0
subject1 observable disposed
subject1 observable disposed
subject2 observable disposed

Which is think wrong because it's strange that observable could be disposed twice. Can someone please explain why is it so?

implementation("io.reactivex.rxjava2:rxjava:2.2.2")

Upvotes: 2

Views: 513

Answers (1)

Farid
Farid

Reputation: 2562

Since it worked for OP, wouldn't hurt to make it answer for future readers, I guess. Basically upgrading RxJava version to 2.2.6 resolves the problem:

implementation 'io.reactivex.rxjava2:rxjava:2.2.6'

Anyone interested in the problem behind this behavior can check out the PR for the issue in RxJava 2.2.2 here https://github.com/ReactiveX/RxJava/pull/6269

Upvotes: 1

Related Questions