toBlockingFirst方法是否可靠? [英] If is toBlockingFirst method reliable?
问题描述
我对方法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()
}
}
根据callSomething
和callElse
应该执行的操作以及是否应该返回某些内容,您也可以在其中使用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屋!