I'm using Room as ORM and here is my Dao interface:
@Dao
interface UserDao {
@Query(value = "SELECT * FROM User LIMIT 1")
fun get(): Single<User?>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun add(profile: User)
}
In other hand I have a method in Repository which should check if the user is login or not, here is the implementation code:
override fun isUserLogin(): Single<Boolean> = userDao
.get().async()
.onErrorReturn { null }
.map { it != null }
Room will throw the following exception if no row matches the query:
Query returned empty result set: SELECT * FROM User LIMIT 1
I want to return null in this case, but when I execute the code it throw an exception with the following messsage:
Value supplied was null
I can't use Optional because Dao is returning Single<User?> so onErrorReturn should return the same type.
How can I check if the user exists or not without changing Dao?