The official Android documentation states that using allowMainThreadQueries() is not recommended because it could lock the UI for a long period of time and trigger an ANR.
But Kotlin coroutines gave us the possibility to perform some operation in the main thread without effectively blocking the UI.
So I'm asking: is it safe to use allowMainThreadQueries() and access the database in a couroutine scope running on the main thread? Like in the following:
// WITH allowMainThreadQueries()
val activityJob = Job()
val mainScope = CoroutineScope(Dispatchers.Main + activityJob)
mainscope.launch {
// access room database and retrieve some data
// update UI with data retrived
}
Or we should stick to the old way of not allowing main thread queries and performing database queries in another thread?
// WITHOUT allowMainThreadQueries()
val activityJob = Job()
val defaultScope = CoroutineScope(Dispatchers.Default + activityJob)
val mainScope = CoroutineScope(Dispatchers.Main + activityJob)
defaultScope.launch {
// access room database and retrieve some data
mainScope.launch {
// update UI with data retrived
}
}
I'm asking because the former way (with allowMainThreadQueries()):
- is much more readable (I can update the UI in the same coroutine context of the functions that access the database, without bearing about starting the UI updates in another coroutine scope)
- allows for simpler error handling
- makes use of only one coroutine scope (so less scopes to care about)
suspendthen you can go with first approach, otherwise second. - Sergey