I need to fetch some data using multiple calls. My code is synchronous, and I can call a synchronous getData method (which makes a network request) multiple times to get everything. To improve performance, I want to execute all these calls concurrently using coroutines, but I don't know if this code is really doing what I want it to.
My relevant Kotlin code looks roughly like:
fun getAllData(): List<Data> {
val coros = listof(1, 2, 3, 4).map {
num -> GlobalScope.async(Dispatchers.IO) { getData(num) }
}
return runBlocking { coros.awaitAll() }
}
I can edit this code, but I cannot edit the getData method.
Is this code running the getData calls concurrently? I'm suspicious that wrapping this in an async block doesn't solve the issue as presumably somewhere inside getData I need to await/return control back to the async block to continue to the next call, which I am not doing since getData is synchronous.
If this isn't running concurrently, is there any way I can make the calls behave asynchronously? Would the overhead of threads typically be worth the time saved in making multiple concurrent network requests?