Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Try this:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading