kevin.cianfarini
06/22/2023, 7:28 PMJoffrey
06/22/2023, 7:35 PMSam
06/22/2023, 7:43 PMtakeWhile
or transformWhile
. A flow isn’t a coroutine and doesn’t have a job, so trying to exit it by “cancelling” something isn’t apropos.
• Yes, there’s a difference between throwing a cancellation exception and cancelling the job. Cancelling the job puts the job into a cancelled state immediately, such that all subsequent attempts to suspend the coroutine will throw a cancellation exception. You can’t recover from it because you can’t unset the job’s cancellation state. Merely throwing a cancellation exception without cancelling the job does not put the job into the cancelled state until the job is actually completed, meaning you can, if you choose, recover from this state by catching the exception and continuing as if nothing had happened.kevin.cianfarini
06/22/2023, 7:50 PMYes, there’s a difference between throwing a cancellation exception and cancelling the job.Perfect! This is the info I needed. 🙂 Thank you.
Unfortunately this wasn’t an option for me. Library code which I don’t control is callingortakeWhile
.transformWhile
.single
on the flow. takeWhile or transformWhile will succeed but not produce a value, and thus single
would fail with no such element exception.Sam
06/22/2023, 7:54 PMkevin.cianfarini
06/22/2023, 7:55 PMkevin.cianfarini
06/22/2023, 7:57 PMkevin.cianfarini
06/22/2023, 7:57 PMkevin.cianfarini
06/22/2023, 7:58 PMkevin.cianfarini
06/22/2023, 7:58 PMSam
06/22/2023, 8:03 PMkevin.cianfarini
06/22/2023, 8:03 PMkevin.cianfarini
06/22/2023, 8:04 PMFlow.single
which is the problem here)Sam
06/22/2023, 8:09 PMSam
06/22/2023, 8:09 PMkevin.cianfarini
06/22/2023, 8:11 PMkevin.cianfarini
06/22/2023, 8:11 PMException
or Throwable
.kevin.cianfarini
06/22/2023, 8:11 PMrunCatching
is a bad API to expose in Kotlin since it breaks cancellationkevin.cianfarini
06/22/2023, 8:14 PMIllegalStateException
. Maybe in Kotlinx-coroutines v2 They’ll opt to have a standalone cancellation exception instead of one that has an inheritance hierarchy