toBlockingFirst方法是否可靠? [英] If is toBlockingFirst method reliable?

查看:371
本文介绍了toBlockingFirst方法是否可靠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对方法toBlockingFirst()的疑问.

这是一种可靠的方法吗?即我可以InterruptedException崩溃

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

如果我打电话要求将处置后的一次性物品丢弃?

if I call dispose for disposable from subscrine?

例如:

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

有人可以向我解释吗?

推荐答案

如果flatMap在RxJava Scheduler上运行,并且调用blockingFirst的时间,您很可能将InterruptedException包裹在.但是,您不应在处理程序中调用阻塞方法,而应通过flatMap

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()
        }
 }

根据callSomethingcallElse应该执行的操作以及是否应该返回某些内容,您也可以在其中使用map doOnNext而不是flatMap.

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.

这篇关于toBlockingFirst方法是否可靠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆