【发布时间】:2020-09-01 20:59:07
【问题描述】:
这个问题与this question 松散相关,但没有答案。 Bob Dalgleish 的回答很接近,但不支持来自 Single 的潜在错误(我认为 OP 实际上也想要)。
我基本上是在寻找一种“过滤错误”的方法——但当查找是基于 RX 时,我认为这不存在。我正在尝试获取值列表,通过查找运行它们,并跳过任何返回查找失败(可抛出)的结果。我无法弄清楚如何以被动方式完成此任务。
我尝试了各种形式的error handling operators 与映射相结合。过滤器仅适用于原始值 - 或者至少我不知道如何使用它来支持我想做的事情。
在我的用例中,我迭代了一个 ID 列表,从远程服务请求每个 ID 的数据。如果服务返回 404,则该项目不再存在。我应该从本地数据库中删除不存在的项目并继续处理 ID。流应返回查找值的列表。
这是一个松散的例子。如何编写 getStream() 以便 canFilterOnError 通过?
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import org.junit.Test
class SkipExceptionTest {
private val data: Map<Int, String> = mapOf(
Pair(1, "one"),
Pair(2, "two"),
Pair(4, "four"),
Pair(5, "five")
)
@Test
fun canFilterOnError() {
getStream(listOf(1, 2, 3, 4, 5))
.subscribeOn(Schedulers.trampoline())
.observeOn(Schedulers.trampoline())
.test()
.assertComplete()
.assertNoErrors()
.assertValueCount(1)
.assertValue {
it == listOf(
"one", "two", "four", "five"
)
}
}
fun getStream(list: List<Int>): Single<List<String>> {
// for each item in the list
// get it's value via getValue()
// if a call to getValue() results in a NotFoundException, skip that value and continue
// mutate the results using mutate()
TODO("not implemented")
}
fun getValue(id: Int): Single<String> {
return Single.fromCallable {
val value: String? = data[id]
if (value != null) {
data[id]
} else {
throw NotFoundException("dat with id $id does not exist")
}
}
}
class NotFoundException(message: String) : Exception(message)
}
【问题讨论】: