Rx java 如何在RxJava2中无提示地跳过异常?
我有这样一个数据流:Rx java 如何在RxJava2中无提示地跳过异常?,rx-java,reactive-programming,rx-java2,rx-kotlin,rx-kotlin2,Rx Java,Reactive Programming,Rx Java2,Rx Kotlin,Rx Kotlin2,我有这样一个数据流: Observable .fromFuture( CompletableFuture.supplyAsync { // First remote call returns Future<List<Type>> listOf(1, 2, 3, 57005, 5) }, Schedulers.computation() ) .flatMap { it.toObs
Observable
.fromFuture(
CompletableFuture.supplyAsync { // First remote call returns Future<List<Type>>
listOf(1, 2, 3, 57005, 5)
},
Schedulers.computation()
)
.flatMap { it.toObservable() } // I turn that list into a stream of single values to process them one by one
.map {
CompletableFuture.supplyAsync { // This remote call may fail if it does not like the input. I want to skip that failures and continue the stream like the fail never occurred.
if (it == 0xDEAD) {
throw IOException("Dead value!")
}
it
}
}
.flatMap {
Observable.fromFuture(it) // Turn that Futures into a stream of Observables once again
}
.doOnNext {
println(it) // Debug
}
.blockingSubscribe()
Observable.fromFuture(it).onErrorResumeNext(Observable.empty())
但如果该“死”值出现在流中,则失败:
1
4
9
Exception in thread "main" java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.io.IOException: Dead value!
at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:45)
at io.reactivex.internal.operators.observable.ObservableBlockingSubscribe.subscribe(ObservableBlockingSubscribe.java:86)
at io.reactivex.Observable.blockingSubscribe(Observable.java:5035)
at by.dev.madhead.rx.TestKt.main(test.kt:41)
Caused by: java.util.concurrent.ExecutionException: java.io.IOException: Dead value!
at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
...
我是RX的新手,所以很快就搜索到了解决方案:OneExceptionResumeNext
:Observable.fromFuture(it)
-->Observable.fromFuture(it).OneExceptionResumeNext{Observable.empty()}
。但是现在我的应用程序永远挂起(在生成我期望的输出之后)。
看起来这条河永远不会结束
我应该以某种方式“关闭”可观察到的吗?
或者,更一般地说,在使用RX时,这是一种好方法吗?
我应该用另一种方式重新思考吗?像这样接受例外:
Observable
.fromFuture(
CompletableFuture.supplyAsync { // First remote call returns Future<List<Type>>
listOf(1, 2, 3, 57005, 5)
},
Schedulers.computation()
)
.flatMap { it.toObservable() } // I turn that list into a stream of single values to process them one by one
.map {
CompletableFuture.supplyAsync { // This remote call may fail if it does not like the input. I want to skip that failures and continue the stream like the fail never occurred.
if (it == 0xDEAD) {
throw IOException("Dead value!")
}
it
}
}
.flatMap {
Observable.fromFuture(it) // Turn that Futures into a stream of Observables once again
}
.doOnNext {
println(it) // Debug
}
.blockingSubscribe()
Observable.fromFuture(it).onErrorResumeNext(Observable.empty())
以下是一些例外情况:
Observable
.fromFuture(
CompletableFuture.supplyAsync { // First remote call returns Future<List<Type>>
listOf(1, 2, 3, 57005, 5)
},
Schedulers.computation()
)
.flatMap { it.toObservable() } // I turn that list into a stream of single values to process them one by one
.map {
CompletableFuture.supplyAsync { // This remote call may fail if it does not like the input. I want to skip that failures and continue the stream like the fail never occurred.
if (it == 0xDEAD) {
throw IOException("Dead value!")
}
it
}
}
.flatMap {
Observable.fromFuture(it) // Turn that Futures into a stream of Observables once again
}
.doOnNext {
println(it) // Debug
}
.blockingSubscribe()
Observable.fromFuture(it).onErrorResumeNext(Observable.empty())
这不会真的打断这条链条并取消对可观察者的订阅吗?@FabioA。Future
已返回,但有一个错误。没有别的事可做了。如果您将其用于反应流,则错误是终端事件,链已经停止。@TassosBassoukos,我误解了这个问题。我认为OP希望忽略错误,但继续处理。我弄错了,谢谢你的回复。这会不会真的打断这条链条并取消订阅《观察者》@FabioA。Future
已返回,但有一个错误。没有别的事可做了。如果您将其用于反应流,则错误是终端事件,链已经停止。@TassosBassoukos,我误解了这个问题。我认为OP希望忽略错误,但继续处理。我错了,谢谢你的回复。