【问题标题】:If is toBlockingFirst method reliable?toBlockingFirst 方法是否可靠?
【发布时间】:2018-04-25 13:40:31
【问题描述】:

关于toBlockingFirst()方法的问题。

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

如果我从 subscrine 调用 dispose 为一次性的?

例如:

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

有人可以解释一下吗?

【问题讨论】:

    标签: java android kotlin rx-java2


    【解决方案1】:

    如果flatMapblockingFirst 被调用时在RxJava Scheduler 上运行,您可能会得到一个InterruptedException 包装在RuntimeException 中。但是,您不应在处理程序中调用阻塞方法,而应通过 flatMap 编写

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

    取决于 callSomethingcallElse 应该做什么,如果他们应该返回一些东西,你也可以在那里有 map doOnNext 而不是 flatMap

    【讨论】:

    • 这是个好主意。所以现在我想我可以暂时避免使用 toBlocking 谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 2015-10-12
    • 2015-11-07
    • 2016-11-01
    相关资源
    最近更新 更多