Search code examples
androidkotlinandroid-roomrx-kotlin

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


I have an function in @Dao. Lets call that class DaoClass

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

Now, I want to check if list of data returned is empty or not. I dug through the DaoClass_Impl (generated at build time) and I found out that the Flowable won't be empty. So,

getData().isEmpty will always return false.

So what I did was getData().singleOrError().map{it.isEmpty()} to return if the returned list is actually empty.

But I am having problem as the value is not getting emitted.


Solution

  • You need to check what singleOrError actually does:

    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.

    What you're looking for is firstOrError, which returns just the first value emitted.