Room: Cannot access database on the main thread since it may potentially lock the UI for a long period of time
A Room DAO query ran on the main thread; Room forbids it by default.
Seen on:
Android
Meaning
Make DAO methods suspend or return Flow/LiveData so Room runs them off the main thread. allowMainThreadQueries() is only for tests.
Common causes
- Non-suspend DAO call from Activity/ViewModel main thread
- Blocking call inside a main-thread coroutine
⚡ Quick fix
- Use suspend DAO functions or Flow
- Call from viewModelScope (Room switches dispatcher)
- Avoid allowMainThreadQueries
Detailed fix by platform
Kotlin
- kotlin
@Dao interface UserDao { @Query("SELECT * FROM user WHERE id = :id") suspend fun get(id: Long): User? @Query("SELECT * FROM user") fun all(): Flow<List<User>> }
How to diagnose
- DAO — suspend/Flow?
- Caller — Thread/dispatcher
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- ActivityNotFoundException ActivityNotFoundException: Unable to find explicit activity class; have you declared this activity in your AndroidManifest.xml?
- Android resource linking failed AAPT: error: resource x (aka y) not found — Android resource linking failed
- android:attr/lStar not found AAPT: error: resource android:attr/lStar not found
Most viewed in Android
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026