how to handle RxAndroid errors in the main thread

泄露秘密 提交于 2019-12-04 04:10:57

I just found out that I only need to change the order of calls. When I call observeOn after timeout it works as expected:

.timeout(1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())

log-output

onNext(one and only)-thread: main
onError()-thread: main

The reason is, that observeOn will only affect everything below the call, and only until some other operator changes the thread again. In the example above, timeout() will change to the computation thread.

Note, that subscribeOn works differently. It does not matter where in the chain you call it.
You should only call it once (when you call it multiple times the first call wins: see "Multiple subscribeOn" in this Blog)

Here is a nice blog-post with more details: RxJava- Understanding observeOn() and subscribeOn()

Because .timeout(1, TimeUnit.SECONDS) by default operates on Schedulers.computation().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!