Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room with RxJava Single<List<T>> compilation error

i’m getting a compilation error “error: Not sure how to convert a Cursor to this method’s return type” when trying to query

Single<List<T>> getList()

but when i change Single to Flowable everything is Ok.

What could be the reason for that error?

like image 789
Griill Avatar asked Jun 18 '26 11:06

Griill


1 Answers

Yes, it provides an error, because you are observing for database changes.

Single emits and requires only 1 result. Since it's a wrapper for LiveData which notify as soon as data has been changed, it requires to use Flowable. This allows to get the data in a Stream even if there are currently no data available.

Also take care, that you should not use generics in classes which are generated. this may lead to unwanted behaviour.

If you really want to wrap the data into a Single you may use in your Dao

@Query("SELECT * FROM ActiveShooter") List<ActiveShooter> getAllActiveShooters(); 

and transform your results into a Single within your repository.

public void Single<List<ActiveShooter>> getActiveShooterInRepo() {
     return Single.fromCallable( () -> yourDao.getAllActiveShooters()); 
}

If you do that you'll loose the observability of data changes.

like image 67
Emanuel S Avatar answered Jun 20 '26 00:06

Emanuel S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!