hooliooo
09/12/2019, 8:38 PMsomeSuspendHttpRequestWithKtor
in parallel from the map
call?
suspend fun someCRUDMethod(models: List<MyModel>) {
val myModels = coroutineScope {
async {
models.map { myAPI.someSuspendHttpRequestWithKtor(it.id) }
}
}
val myListOfModels = myModels.await()
....
}
streetsofboston
09/12/2019, 8:41 PMsuspend fun someCRUDMethod(models: List<MyModel>) {
val myListOfModels = models.map { myAPI.someSuspendHttpRequestWithKtor(it.id) }
....
}
someSuspendHttpRequestWithKtor
in parallel, one async for each item in models
?// someSuspendHttpRequestWithKtor executed in parallel
suspend fun someCRUDMethod(models: List<MyModel>) {
val myDeferredModels = coroutineScope {
models.map { async { myAPI.someSuspendHttpRequestWithKtor(it.id) } }
}
val myListOfModels = myDeferredModels.awaitAll()
....
}
hooliooo
09/12/2019, 8:44 PMstreetsofboston
09/12/2019, 8:44 PMhooliooo
09/12/2019, 8:45 PMstreetsofboston
09/12/2019, 8:46 PMhooliooo
09/12/2019, 8:46 PMstreetsofboston
09/12/2019, 8:48 PMawaitAll()
, in this example, does not really wait at all… it already has all the results, because coroutineScope [ ... ]
only resumes/returns when all its children (calls to async
) have finished. Still, the effect is the same in your example.hooliooo
09/12/2019, 8:53 PMawaitAll()
just unboxes them from their Deferred wrapper?streetsofboston
09/12/2019, 8:55 PMcoroutineScope
has resumed and that can only happen if all its (async) children have finished), it doesn’t really wait in this caseawaitAll()
will await until all of them have finished.hooliooo
09/12/2019, 8:56 PM