简体   繁体   中英

Return a query without LiveData

Basically what I want to do is when I open my app It'll make a query to get a value from the database and use it.

I'm using Room and when I try not to use LiveData it tells me Cannot access database on the main thread since it may potentially lock the UI for a long period of time , so I wonder how could I avoid using LiveData, because if I use it (if I understood right) I can't just pick the information from the database - I have to wait until something changes and only then I'd have access to the query results.

So I found a simple solution.

Because I just want this query used once - on app startup, I just enabled Room database to use the main thread using the allowMainThreadQueries() function.

You're welcome to post here your solution if you think it's better, it would be appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM