Home > database >  Android Room Database Query without using Flow or LiveData (no observing)
Android Room Database Query without using Flow or LiveData (no observing)

Time:10-01

I'm trying to do a check in my apps Room DB. I want to check if a pin code has been used by querying the DB to find any results with that pin.

My DAO function:

@Query("SELECT * FROM tblUser WHERE userPin = :pin LIMIT 1")
fun checkIfUserPinIsUsed(pin: String): UserTable?

I want to do this without having to observe that result because my query is executed when a button is clicked (validation is done).

How do I achieve this without getting the 'Cannot access database on the main thread' error?

CodePudding user response:

Try this:

Room.databaseBuilder(context, TableName.class, context.getResources().getString(R.string.db_name))
            .allowMainThreadQueries() // add this line
            .build();
  • Related