如何将Flowable转换为Single?或者,如果有另一种方法使其在第一个感兴趣的响应之后停止发光。
我试过这个,但它似乎不起作用:
disposables.add(
repository.getAllSomethings()
.subscribeOn(SchedulerProvider.getInstance().computation())
.observeOn(SchedulerProvider.getInstance().ui())
.toSingle()
.subscribeWith(object : DisposableSingleObserver<List<Something>>() {
override fun onSuccess(t: List<Something>) {
}
override fun onError(e: Throwable) {
}
})
getAllSomethings()返回一个Flowable<List<Something>>
在上面的代码中,.subscribeWith()
用红色下划线表示:
Type parameter bound for E in
fun <E : SingleObserver<in Flowable<List<Something>!>!>!> subscribeWith(observer: E!): E!
is not satisfied: inferred type ! is not a subtype of SingleObserver<in Flowable<List<Something>!>!>!
好的,感谢@akarnokd,我在评论中看到了答案。
教我如何钓鱼,而不是给我他建议在这里看到的直接答案:http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html(我本来应该做的第一个!)。
在那里我发现firstOrError()
解决了我的问题。
即使我从toSingle()
调用的对象是Flowable,IDE也没有抱怨。然而,看看上面的链接,toSingle()
甚至不是一个有效的选择!