Deadlock with runBlocking(UI)



I try to manage my threads for IO Processes. One of is for Realm usage.


init {
        val ai = app.packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
        bundle = ai.metaData
        runBlocking(appExecutors.dbContext) {

It works fine if I set val dbContext as newSingleThreadContext(“databaseIO”)…
But I develop an android-library, so If there is an implementation of Realm on the app module, I need to set usage on the same thread. And generally, everyone use main thread to use Realm. In that case I tried to set UI but it caused ANR. I can understand why it causes ANR, but I can’t find a proper solution for this scenario.

Note: if I use it with launch… it works for here. But on my RealmManager class I need to use runBlocking. So there is no way to use only launch…:slight_smile:

fun getProfile(id: String): Profile? {
        try {
            return runBlocking(dbCoroutine) {
                val query = realm!!.where("", id)
        } catch (ex: Exception) {
            logger.e(TAG, ex)
            return null


internal val allProfiles: List<Profile>
        get() = runBlocking(dbCoroutine) { realm!!.where( }

Is there anything I do wrong way, or any advice for better implementation?

Thanks & Regards


The UI context will post a task to the handler of the UI thread. Handlers process messages in sequence. If you call getProfile from the UI thread this means that your function never returns because you are waiting for the task to complete but it never will because for it to be processed the function needs to return (and give control to the handler so it can process messages).