https://kotlinlang.org logo
#coroutines
Title
# coroutines
v

v0ldem0rt

03/28/2019, 3:11 PM
If I have something like
Copy code
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).async {
        // Do some work
    }
Would this link the scope that I just created to parent scope (i.e. would parent wait)
a

ahulyk

03/28/2019, 3:13 PM
what do you mean?
Copy code
launch {
                val data = async(IO) {
                    dataRepository.getData()
                }
                data.await()
}
s

streetsofboston

03/28/2019, 3:17 PM
I don’t think it does. Do this instead to be sure the parent scope’s context is inherited
Copy code
async(context = <http://Dispatchers.IO|Dispatchers.IO>) {
        // Do some work
    }
v

v0ldem0rt

03/28/2019, 3:18 PM
Actually no, if I don't do await on the Deferred coming back. Would my caller scope wait for child scope to terminate? i.e. Does the async body acts like if I were to launch it in
GlobalScope.async
. I am trying to make it behave like
GlobalScope
g

gildor

03/28/2019, 3:33 PM
No, it will not, there is no Parent/Child relationships, new Job will be implicitly created
You can pass job (or context) from parent scope, but this style is not recommended
v

v0ldem0rt

03/28/2019, 3:59 PM
👍
13 Views