I created a generic function to run a few network calls parallely and then collect their results.
fun <T> runAsync(process: () -> T, coroutineScope: CoroutineScope): Deferred<T> {
return coroutineScope.async(Dispatchers.IO + RequestCoroutineContext() + SecurityCoroutineContext()) {
process()
}
}
Now I use this method to be used to make network calls and collect their results like this ->
runBlocking {
// async is the same method that is written above
val job1 = runAsync({ netWorkCallOne(params) }, this)
val job2 = runAsync({ netWorkCallTwo(params) }, this)
val job3 = runAsync({ netWorkCallThree(params) }, this)
val job4 = runAsync({ netWorkCallFour(params) }, this)
val job5 = runAsync({ netWorkCallFive(params) }, this)
val job6 = runAsync({ netWorkCallSix(params) }, this)
someData4 = job5.await()
someData5 = job6.await()
someData3 = job4.await()
someData2 = job3.await()
someMoreData = job2.await()
someData = job1.await()
}
I can see in the logs that no two calls are happening sequentially waiting for one network call to finish and then the next call starts. What can I change here to make it run parallely?