I am experimenting a bit with kotlin coroutines, and I asked my self, how to perform multiple work inside one and the same suspend function (e.g call the network and the local database at the same time). Researching this problem led me to the following solution:
suspend fun doWorkInParallel() {
coroutineScope {
val networkCall = async { // stuff }
val databaseCall = async { // stuff }
awaitAll(networkCall, databaseCall)
}
}
Here are my questions:
- Is using the above approach correct or is there another way in performing parallel work in one and the same suspend function
- Does the other "coroutineScope" leak anything when I call the suspend function?
- Is the work done in parallel?
- What happens, when both the
networkCalland thedatabaseCallaccess one and the same object / change one and the same object (e.g inside thenetworkCalla variable is set toState Xand inside thedatabaseCallthe variable is set toStata Y)