svenjacobs
02/14/2020, 8:39 AMCoroutineScope
that keeps on running even if the scope is cancelled and if another (new) scope is accessing the same function/computation, the already computed value should be returned? Sounds like a job for a ConflatedChannel
, right? I tried something with Deferred
but the code (test code!!) is far from beautiful:
var result: Deferred<Int>? = null
fun longComputationAsync(): Deferred<Int> {
result = result ?: GlobalScope.async {
println("longComputation")
delay(1000)
1337
}
return result!!
}
val scope1 = CoroutineScope(Dispatchers.Default)
scope1.launch {
println("scope 1")
println(longComputationAsync().await())
}
scope1.cancel()
val scope2 = CoroutineScope(Dispatchers.Default)
val job = scope2.launch {
println("scope 2")
println(longComputationAsync().await())
}
runBlocking {
job.join()
}
Matej Drobnič
02/14/2020, 8:40 AMMatej Drobnič
02/14/2020, 8:40 AMMatej Drobnič
02/14/2020, 8:41 AMMatej Drobnič
02/14/2020, 8:41 AMsvenjacobs
02/14/2020, 8:42 AMresult
in the outer scope so that Deferred
should be shared, right? This is just a simulation of a singleton object which might do the computation.bohsen
02/14/2020, 8:58 AMsvenjacobs
02/14/2020, 9:09 AMNonCancellable
solution for instance is not what I actually need. Using NonCancellable
would mean that the computation is finished in scope1
however in my use case the result should land in scope2
uli
02/14/2020, 9:10 AMimport kotlinx.coroutines.*
val result by lazy { longComputationAsync() }
fun longComputationAsync(): Deferred<Int> {
return GlobalScope.async {
println("longComputation")
delay(1000)
1337
}
}
val scope1 = CoroutineScope(Dispatchers.Default)
scope1.launch {
println("scope 1")
println(result.await())
}
scope1.cancel()
val scope2 = CoroutineScope(Dispatchers.Default)
val job = scope2.launch {
println("scope 2")
println(result.await())
}
runBlocking {
job.join()
}