dave08
07/28/2019, 2:47 PMsuspend fun <K: Any, V> AsyncCache.getSuspending(key: K): V {
val outerContext = coroutineContext
return get(key) { k, executor ->
val innerContext = outerContext + Job() + executor.asCoroutineDispatcher()
CoroutineScope(innerContext).async {
loadValue(k) // loadValue is a suspend function defined elsewhere
}.asCompletableFuture()
}.await()
}
Dominaezzz
07/28/2019, 2:58 PMsuspend fun <K: Any, V> AsyncCache.getSuspending(key: K): V {
val outerContext = coroutineContext
return get(key) { k, executor ->
val innerContext = outerContext + Job() + executor.asCoroutineDispatcher()
GlobalScope.async(innerContext) {
loadValue(k) // loadValue is a suspend function defined elsewhere
}.asCompletableFuture()
}.await()
}
suspend fun <K: Any, V> AsyncCache.getSuspending(key: K): V {
val outerContext = coroutineContext
return get(key) { k, executor ->
val innerContext = outerContext + Job() + executor.asCoroutineDispatcher()
GlobalScope.future(innerContext) {
loadValue(k) // loadValue is a suspend function defined elsewhere
}
}.await()
}
dave08
07/28/2019, 3:00 PMGlobalScope
)? Or did I misunderstand?innerContext
does the same?Dominaezzz
07/28/2019, 3:01 PMCoroutineScope
without explicitly cancelling it is the same as GlobalScope
. Might as well save the allocation.dave08
07/28/2019, 3:05 PM