https://kotlinlang.org logo
Title
p

Patrick Steiger

04/03/2023, 8:22 PM
Is there something similar to
yield
that does not dispatch if
isDispatchNeeded == false
?
Context is I’m starting a coroutine with
CoroutineStart.UNDISPATCHED
but at some point I want to check for cancellation and run the code in the correct coroutine context present in scope, but I want to avoid dispatch if dispatcher is immediate and it was already called in the right thread
b

bezrukov

04/03/2023, 8:32 PM
If you are only interested in cancellation, you may use
ensureActive
instead https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/ensure-active.html
p

Patrick Steiger

04/03/2023, 8:58 PM
@bezrukov no, because it’s started undispatched (random caller thread) and ensureActive would not dispatch it if needed