【发布时间】:2022-08-03 17:24:38
【问题描述】:
TL;博士
我认为flatMap 与switchMap 结合可能无法正确终止流,因此会发生UndeliverableException。我怎样才能解决这个问题?
结构
我正在制作一些复杂的流——结合flatMap 和switchMap——如下所示,在 RxKotlin (RxJava 3) 中:
someObservable
.flatMapMaybe {
if (matchCondition(it)) Maybe.just(it)
else Maybe.never()
}.flatMapSingle {
procedureMiddle(it) // Inconsistent-time-consuming Single
}.switchMap {
procedureLater(it)
}.doOnError {
dealWithError(e)
}.retry()
.subscribeBy(
// ...
)
flatMapSingle 内部的 procedureMiddle 最终有可能返回错误。
例外
事实证明,有时来自procedureMiddle 的错误可能会跳出结构,不会被retry 忽略,也不会在doOnError 中的dealWithError 中处理:
W/System.err: io.reactivex.rxjava3.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What\'s-different-in-2.0#error-handling | my.custom.app.CustomException
// traces indicates that the Exception is thrown from inside procedureMiddle
问题
事实上,UndeliverableException 并不会真正导致崩溃,但它对我来说有点烦人——尤其是它看起来像是我需要处理的情况。但是我认为结构写得正确吗?所以这是我的问题:
-
switchMap真的(正确地)终止了来自flatMap的最后一个流吗? (并且可以用来防止UndeliverableException?) - 如果是这样,我应该在代码的哪一部分进行调整?如果不是这样,我该如何防止异常以及我的结构? (我想在
procedureMiddle之后连接procedureLater,并且只保留最新的一个)任何建议或解释都会有所帮助。