我有一个像这样的简单流:
Observable.error<Int>(Exception()).startWith(1).subscribe {
println("Item is $it")
}
一切都按预期进行。首先使用整数 1 调用 onNext,然后抛出异常,但是当我通过添加observeOn 来更改流时:
Observable.error<Int>(Exception()).startWith(1).observeOn(AndroidSchedulers.mainThread()).subscribe {
println("Item is $it")
}
onNext 永远不会被调用。仅抛出异常。我在这里缺少什么?
最佳答案
来自observeOn文档
Note that onError notifications will cut ahead of onNext notifications on the emission thread if Scheduler is truly asynchronous.
这意味着当您应用它时,首先会发出 onError,因此不会调用 onNext,因为流由于 onError 而结束。
您可以执行以下操作以便首先接收 onNext
observeOn(AndroidSchedulers.mainThread(), true)
这告诉Observable
延迟错误直到startWith的onNext
被传递
关于android - 与observeOn组合时RxJava的startsWith操作符被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49554485/