What does Room return if a query had no results

Viewed 2296

Having a difficult time finding this answer and the documentation doesn't seem to answer the question. If I have a basic ROOM query,

@Query("SELECT * FROM geotable WHERE geohash = :geohash")
abstract suspend fun getGeoTable(geohash: String) : GeoTable

and there is no such item that uses this primary key, what happens? Android studio says that the DAO object will never return a null. It seems that EmptyResultSetException only gets thrown when you have Single using RxJava which I am not using. So what does plain old ROOM throw when it finds nothing?

4 Answers

Based on the documentation a fruitless query's behavior is defined by the declared return type:

  1. fun func(foo: Foo): Bar would throw a EmptyResultSetException
  2. fun func(foo: Foo): Bar? would return null
  3. fun func(foo: Foo): List<Bar> would return an empty list

If nothing is found based on your criteria, it will return null.

By looking at how Daos code is generated in the respective *Dao_Impl, your Daos implementation should be contain something similar:-

final GeoTable result;
if(_cursor.moveToFirst()) {
   ....
}else {
    _result = null;
    }
return _result;
}

So if there are no records in the cursors result set, the _cursor.moveToFirst() should return false and eventually result in returning a null.

if there are no results it returns nothing simply and nothing happens

Related