simon.vergauwen
01/03/2019, 7:27 PMDeferred<A>
NonCancelable
by wrapping and awaiting it in a withContext
block like below. And how does its scope get (re)wired correctly? Snippet seems to work although I don't quite understand it.
val def = thisWasPassedToMe()
val result = withContext(NonCancelable) {
def.await()
}
bdawg.io
01/03/2019, 7:31 PMCancellationException
thrown if the backing Job for def
gets cancelledNonCancellable
will just make sure that your withContext
itself doesn’t exit from being cancelled itselfsimon.vergauwen
01/03/2019, 7:34 PMwithContext
is NonCancelable?bdawg.io
01/03/2019, 7:44 PMDeferred
is just a reference handle to the state of the coroutinesimon.vergauwen
01/03/2019, 7:51 PMgroostav
01/03/2019, 9:06 PMlaunch
or runBlocking
you can use CoroutineStart.ATOMIC
launch(CoroutineStart.ATOMIC){
val def = thisWasPassedToMe()
def.await()
}
suspend fun doStuffWithThingPassedToMe(){
try {
val def = thisWasPassedToMe()
}
finally {
//this will fire in all cercomstances (except abandonment!)
}
}
simon.vergauwen
01/03/2019, 9:13 PMDeferred
is guaranteed to have never run.groostav
01/03/2019, 9:23 PMrelease
?simon.vergauwen
01/03/2019, 9:26 PMDeferred<A>
that has never run and is lazily run.release
is guaranteed to be called. There is also a use
function which you can use to consume the `acquire`d resource.Deferred
to safely consume and release resources with a guarantee not to leak.Observable
, Flux
, IO
, …