zak.taccardi
04/08/2019, 6:14 PMCoroutineScope
?zak.taccardi
04/08/2019, 6:14 PMGlobalScope
works, but is it recommend?Allan Wang
04/08/2019, 6:14 PMNonCancellable
context?Allan Wang
04/08/2019, 6:16 PMGlobalScope
seems reasonablezak.taccardi
04/08/2019, 6:17 PMNonCancellable
and GlobalScope
?simon.vergauwen
04/08/2019, 6:21 PMsimon.vergauwen
04/08/2019, 6:21 PMAllan Wang
04/08/2019, 6:24 PMNonCancellable
, you can wrap part of your execution with it, and leave the remaining executions cancellable. In that case, your full job will cancel prematurely if it is cancelled, just not within the noncancellable scope. You can probably still do this with GlobalScope
though.Allan Wang
04/08/2019, 6:25 PMzak.taccardi
04/08/2019, 6:25 PM// invoked within a cancellable scope
processStarter.start { result ->
// even if the scope is cancelled, this launch coroutine MUST be executed
launch {
// suspending function, which is why the `launch` above is needed
mustDeliverResult(result)
}
}
the start { .. }
lambda is not tied to any coroutine and is just a callback that once returned, must deliver its resultzak.taccardi
04/08/2019, 6:28 PMlaunch(NonCancellable + Dispatchers.Unconfined) {
mustDeliverResult(result)
}
and
GlobalScope.launch(Dispatchers.Unconfined) {
mustDeliverResult(result)
}
I’m also using Dispatchers.Unconfined
too because I want that result delivered ASAP, and not posted to an executor or queued up.
I’m not sure why I might choose one solution over the otherAllan Wang
04/08/2019, 6:35 PMGlobalScope.launch
.
If your function is a suspended function and you want to make it noncancellable, it’s easier to use withContext(NonCancellable) { ... }
You have the liberty of using either since you are in a scope.simon.vergauwen
04/09/2019, 12:43 PMwithContext
within another scope, right?zak.taccardi
04/09/2019, 1:19 PMstart { .. }
is a (ProcessResult) -> Unit
, so it's not being called from any suspending contextsimon.vergauwen
04/09/2019, 1:31 PM