Mani
10/20/2019, 9:12 AMval allWarehouses: List<Warehouse> = getWarehouses()
GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
allWarehouses.forEach { warehouse ->
val notes = getNotes()
// I want to run this loop in parallel
notes.forEach {
serviceClient.markDone(note) // this is an IO call
}
}
}
I want to run the forEach operation in parallel. Is this the right way of doing?Allan Wang
10/20/2019, 9:26 AMmarkDone
will not run in parallel. This job however will be sent to an io thread relative to the contents outside of the launch. If you want to run all markDone
in parallel, launch a separate task for each one.Mani
10/20/2019, 9:27 AMlaunch {}
?Allan Wang
10/20/2019, 9:41 AMgildor
10/20/2019, 9:44 AMasync { }
using map:
notes.map { async { markDone() } }.awaitAll()Mani
10/20/2019, 9:57 AMlaunch(<http://Dispatchers.IO|Dispatchers.IO>)
in the right place?markDone
request fails, what will happen to others?gildor
10/20/2019, 11:16 AM