有人能帮帮忙吗?
我有这些函数
fun getBooks(): Single<List<Book>> {
return getCollections()
.map {
it.map(::collectonToBook)
}
}
fun getCollections(): Single<List<Collection>> {
return db.fetchCollections()
.filter(::isBook)
}
fun collectonToBook(collection: Collection): Maybe<Book> {
return collection.toBook()
}问题是当我需要Single<List<Book>>时,getBooks会返回Single<List<Maybe<Book>>>。我是否可以在不调用blockingGet的情况下在流中执行此操作?
发布于 2020-12-09 21:05:59
试试这个:
getCollections() // Single<List<Collection>>
.flattenAsFlowable { it } // Flowable<Collection>
.concatMapMaybe { collectonToBook(it) } // Flowable<Book>
.toList() // Single<List<Book>>换句话说,将内部的List解包到它的元素中,将Collection转换成Book,连接它们各自的Maybe源,最后再将Book收集到List中。
https://stackoverflow.com/questions/65217193
复制相似问题