Kotlin Coroutines - How to block to await/join all jobs?

后端 未结 3 1165
滥情空心
滥情空心 2021-01-18 07:17

I am new to Kotlin/Coroutines, so hopefully I am just missing something/don\'t fully understand how to structure my code for the problem I am trying to solve.

Essen

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 07:28

    Runblocking should mean you don't have to call join. Launching a coroutine from inside a runblocking scope should do this for you. Have you tried just:

    fun processData(lstInputs: List): List {
    
    val lstOfReturnData = mutableListOf()
    
    runBlocking {
        lstInputs.forEach {
                launch(Dispatchers.IO) {
                    lstOfReturnData.add(networkCallToGetData(it))
                }
       } 
    }
    
    return lstofReturnData
    

提交回复
热议问题