RxJava:与.subScribeOn结合使用时,withLatestFrom运算符出现问题

时间:2019-12-10 13:00:19

标签: kotlin rx-java rx-java2

请找到下面的kotlin代码段:

val dummyApi: PublishSubject<String> = PublishSubject.create()

fun withLatestFromtest() {
    dummyApi.onNext("1")
    getOtherObservable().withLatestFrom(dummyApi)
            .subscribeOn(Schedulers.io())
            .subscribeBy(
                    onNext = {
                        Log.e("TAG", "Pair ${it.first} /// ${it.second}")
                    },
                    onError = {
                        it.printStackTrace()
                    },
                    onComplete = {
                        Log.e("TAG", "Pair2 Complete")
                    }
            )
    dummyApi.onNext("2")
    dummyApi.onNext("3")
}

private fun getOtherObservable(): Observable<String>{
    return Observable.just("API")
            .delay(2, TimeUnit.SECONDS)
}

预期输出-

Pair API /// 3
Pair2 Complete

实际输出-

Pair2 Complete

当我删除 .subscribeOn(Schedulers.io())时,它可以正常工作。我不明白为什么调度程序会造成此问题。

0 个答案:

没有答案