toBlockingFirst方法是否可靠?

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.