Shmuel Rosansky [G]
01/08/2020, 10:32 PMoctylFractal
01/08/2020, 10:34 PMsuspend
language feature itself does not know about cancellation, that's strictly a concept from the coroutines library. so it does not check for cancellation.
many (but not all) functions provided by the coroutines library do check for cancellation, and the docs will say if they do or notShmuel Rosansky [G]
01/08/2020, 10:39 PMoctylFractal
01/08/2020, 10:40 PMwithContext
is one of them: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-context.htmlNote that the result of withContext invocation is dispatched into the original context in a cancellable way, which means that if the original coroutineContext, in which withContext was invoked, is cancelled by the time its dispatcher starts to execute the code, it discards the result of withContext and throws CancellationException.
Shmuel Rosansky [G]
01/08/2020, 10:42 PMoctylFractal
01/08/2020, 10:42 PMwithContext(IO)
?withContext
, in short withContext
checks for cancellation before it returnsShmuel Rosansky [G]
01/08/2020, 10:43 PMoctylFractal
01/08/2020, 10:45 PMThe method may throw a CancellationException if the current job was cancelled externally
Shmuel Rosansky [G]
01/08/2020, 10:52 PM