简体   繁体   English

为什么 RxJava Observable.doOnDispose 可以触发两次?

[英]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.这是错误的,因为 observable 可以被处理两次很奇怪。 Can someone please explain why is it so?有人可以解释为什么会这样吗?

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

Since it worked for OP, wouldn't hurt to make it answer for future readers, I guess.因为它适用于 OP,所以我想为未来的读者提供答案不会有什么坏处。 Basically upgrading RxJava version to 2.2.6 resolves the problem:基本上将 RxJava 版本升级到 2.2.6 解决了这个问题:

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任何对此行为背后的问题感兴趣的人都可以在此处查看 RxJava 2.2.2 中问题的 PR https://github.com/ReactiveX/RxJava/pull/6269

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM