https://kotlinlang.org logo
Title
j

juliocbcotta

04/12/2022, 12:32 PM
So, I have a list of tasks that I need to run based on their priority when the android app is launched. I would like to make the Main Thread wait the finish of these tasks execution, but I would like to delegate the execution to background threads....
runBlocking(Dispatchers.Main.immediate) {
    initialize(application)
}
suspend fun initialize(app: Application) = coroutineScope {
    registry.groupBy { initializer ->
        initializer.priority().ordinal
    }
        .toSortedMap()
        .forEach { entry ->
            val tasks = entry.value.map { initializer ->
                async {
                    initializer.initialize(app)
                }
            }
            tasks.awaitAll()
        }
}
The code above runs, but if I change
async {
to
async(Dispatchers.Default) {
Makes the app blocks forever. Would anyone have an idea of how to fix it ?
@elizarov is this an unsolved problem with coroutines ? Is this related to this issue ? Like, I do need a way of make the UI thread wait the execution of those tasks (today I am running everything in the Main Thread). Is there any other mechanism that I could use apart from
runBlocking
?