将 Flowable<List<T>> 转换为 Single<Boolean>

Converting Flowable<List<T>> to Single<Boolean>

我在 @Dao 中有一个函数。让我们称之为 class DaoClass

abstract fun getData() : Flowable<List<Data>>

现在,我想检查 returned 数据列表是否为空。我仔细研究了 DaoClass_Impl(在构建时生成),发现 Flowable 不会为空。所以,

getData().isEmpty 将始终 return 错误。

所以我所做的是 getData().singleOrError().map{it.isEmpty()} 到 return 如果 returned 列表实际上是空的。

但是我遇到了问题,因为没有发出值。

您需要检查 singleOrError 的实际作用:

Returns a Single that emits the single item emitted by this Flowable, if this Flowable emits only a single item, otherwise if this Flowable completes without emitting any items a NoSuchElementException will be signaled and if this Flowable emits more than one item, an IllegalArgumentException will be signaled.

您要查找的是 firstOrError,这 returns 只是发出的第一个值。