If is toBlockingFirst method reliable?

那年仲夏 提交于 2019-12-24 15:27:45

问题


My question about the method toBlockingFirst().

Is it a reliable method? i.e. Can I get InterruptedException with crash

if I call dispose for disposable from subscrine?

for example:

.flatMap{ host ->
    val count = userRepository.getUsers(PrefProvider.currentTourCode)
        .map { it.size }
        .blockingFirst()
    if (count>2) {
        callSomething()
    } else {
        callElse()
    } 
}

Can someone to explain me please?


回答1:


If flatMap runs on an RxJava Scheduler the time blockingFirst is invoked, you'll likely get an InterruptedException wrapped into a RuntimeException. However, you should not call blocking methods in a handler but compose via flatMap

.flatMap{ host ->
    userRepository.getUsers(PrefProvider.currentTourCode)
        .flatMap { 
            if (it.size) {
               return callSomething()
            }
            return callElse()
        }
 }

Depending on what callSomething and callElse are supposed to do and if they should return something, you could also have map doOnNext instead of flatMap in there.



来源:https://stackoverflow.com/questions/50023866/if-is-toblockingfirst-method-reliable

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