问题
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