Hi i was watching a Udemy course on coroutine cooperative cancellation and they showed `isActive` as...
t
Hi i was watching a Udemy course on coroutine cooperative cancellation and they showed
isActive
as an approach to "join in with cancellation", however
isActive
is now showing as deprecated what can/should I use now?
s
The
isActive
extension of
CoroutineScope
isn’t deprecated, this one is fine. The one on
FlowCollector
is, here. You can follow the steps inside the deprecation warning to see your alternatives. You can even auto-fix this problem with the IDE.
t
cheers @Stylianos Gakis a common mistake ...I Had found
yield()
which seemed to do the trick, is that wrong?
s
Hm that should work as well, it does check for cancellation. It’s just that yield() as I understand it does more than just that as indicated by its documentation. Something about giving a chance for other coroutines in the same dispatcher to run. While
currentCoroutineContext().isActive
will just check for cancellation. This might matter if you somehow rely on that piece of code not suspending to wait for something else. Practically speaking, in like 99% of the use cases my guess is that it doesn’t matter though 😅 If some coroutine expert would like to chime in to say why I’m wrong I’d love to hear it
🙌 1
t
thanks for the detail im using
yield()
in my Andriod background workers,
androidx.work.CoroutineWorker
, so from what you are saying i am good if i understand correctly 😄
s
I haven’t used a
CoroutineWorker
before, but yes, yield() does check for cancellation so you’re good 😄
👍 1