rocketraman
04/01/2019, 9:17 PMasync
block. I'm assuming this is the right way?
CoroutineScope(coroutineContext).async { ... }
streetsofboston
04/01/2019, 9:18 PMsuspend fun myFunction(...) = coroutineScope {
...
async { ... }
}
rocketraman
04/01/2019, 9:19 PMcoroutineScope
automatically inherits the callers scope?streetsofboston
04/01/2019, 9:19 PMrocketraman
04/01/2019, 9:19 PMrocketraman
04/01/2019, 9:19 PMstreetsofboston
04/01/2019, 9:19 PMstreetsofboston
04/01/2019, 9:20 PMstreetsofboston
04/01/2019, 9:20 PMlaunch
or async
which need a CoroutineScoperocketraman
04/01/2019, 9:21 PMrocketraman
04/01/2019, 9:22 PMstreetsofboston
04/01/2019, 9:23 PMbdawg.io
04/01/2019, 11:12 PMasync
coroutine is completed
suspend fun myFunction() = coroutineScope {
async { doSomethingSlow() }
}
rocketraman
04/01/2019, 11:14 PMlaunch
.bdawg.io
04/01/2019, 11:16 PMlaunch
does the same thing. Any coroutines started inside of a coroutineScope { ... }
will prevent the coroutineScope
function from returning until all children coroutines have completedbdawg.io
04/01/2019, 11:17 PMcoroutineScope
is better if it works for what you’re doing 🙂rocketraman
04/01/2019, 11:18 PMasync
anyway. But theoretically speaking, what's the alternative if one did want the fire-and-forget behavior, but still run it within the parent scope?bdawg.io
04/01/2019, 11:22 PMCoroutineScope
as a parameter (usually as an extension, see the `launch`/`async` implementations). fun CoroutineScope.myFunction() = async { doSomethingSlow() }
rocketraman
04/01/2019, 11:23 PMrocketraman
04/01/2019, 11:25 PMgildor
04/01/2019, 11:25 PMsuspend
rocketraman
04/01/2019, 11:29 PMprivate suspend fun CoroutineScope.foo(): String {
val b1 = async {
bar()
}
val b2 = async {
bar()
}
return b1.await() + b2.await()
}
private suspend fun bar(): String {
TODO()
}
rocketraman
04/01/2019, 11:31 PMbdawg.io
04/01/2019, 11:33 PMsuspend
being used in conjunction of a CoroutineScope
receiver. All suspend functions have a coroutineContext
property that can be read, so it’s warning about the shadowing that occurs with that afaik.bdawg.io
04/01/2019, 11:34 PMcoroutineScope { ... }
instead (which is the point of the OP) 🙂rocketraman
04/01/2019, 11:36 PMbdawg.io
04/01/2019, 11:49 PMcoroutineContext
is what’s shadowed, not the CoroutineScope
that async
needs, which doesn’t exist without a) it being passed or b) using the coroutineScope
methodrocketraman
04/01/2019, 11:51 PMcoroutineContext
isn't directly usable by async
, so why the warning on async
? The separate warning on CoroutineScope
makes sense.rocketraman
04/01/2019, 11:52 PMstreetsofboston
04/02/2019, 12:04 AMsuspend fun xxxx(...)
that suspends or do fun CoroutineScope.xxxx(...)
that must return immediately. Don't mix them.
If your suspend fun
needs to do something parallel, using launch or async, use = coroutineScope { ... }
rocketraman
04/02/2019, 12:06 AMbdawg.io
04/02/2019, 3:49 AMasync
. The warning is “Ambiguous coroutineContext due to CoroutineScope receiver of suspend function” and it’s ambiguous because suspending functions have a coroutineContext
and having a receiver of CoroutineScope
shadows that with the scope’s coroutineContext
. It’s just manifesting that with async
as well because it utilizes the context, but structured concurrency requires that to go through a scope.