amrelmasry
08/06/2018, 7:35 PMyield()
is doing?
@Test
fun testLaunchAndYieldJoin() = runTest {
expect(1)
val job = launch(coroutineContext) {
expect(3)
yield()
expect(4)
}
expect(2)
assertTrue(job.isActive && !job.isCompleted)
job.join()
assertTrue(!job.isActive && job.isCompleted)
finish(5)
}
withoutclass
08/06/2018, 7:44 PMamrelmasry
08/06/2018, 7:52 PMThere are two approaches to making computation code cancellable. The first one is to periodically invoke a suspending function that checks for cancellation. There is a yield function that is a good choice for that purpose.
which I don't totally understand, is its total purpose to check if a coroutine is cancelled ?amrelmasry
08/06/2018, 7:53 PMbuildSequence
withoutclass
08/06/2018, 7:56 PMyield
Yields a thread (or thread pool) of the current coroutine dispatcher to other coroutines to run. If the coroutine dispatcher does not have its own thread pool (like Unconfined dispatcher) then this function does nothing, but checks if the coroutine Job was completed. This suspending function is cancellable. If the Job of the current coroutine is cancelled or completed when this suspending function is invoked or while this function is waiting for dispatching, it resumes with CancellationException.
amrelmasry
08/06/2018, 8:01 PMwithoutclass
08/06/2018, 8:03 PMwithoutclass
08/06/2018, 8:04 PMamrelmasry
08/06/2018, 9:28 PMgildor
08/07/2018, 1:22 AMyield()
and have automatica cancellation supportamrelmasry
08/07/2018, 12:13 PMwithoutclass
08/07/2018, 4:15 PMisActive
flag, which represents whether the coroutine is still active.