Wondering if someone could explain the difference ...
# android
b
Wondering if someone could explain the difference between the suspending functions,
Lifecycle.whenResumed
vs
Lifecyle.withResumed
? .. it seems like they both work the same way?
u
b
@Robert Williams thanks for the stackoverflow link. That helps!
a
that stackoverflow answer is incorrect in a key way: the
with*
methods accept blocks that cannot suspend, so the case the SO answer describes about resuming from suspension not guaranteeing lifecycle state can't happen
1
the
when*
methods are a source of bugs that we should have deprecated a long time ago. Since they use a pausing dispatcher that doesn't run continuations outside of lifecycle and instead defers them, that means that things like exception handling, notably CancellationExceptions, are also deferred until you're back in lifecycle again or the lifecycle is destroyed
1
in the kind of code people write with coroutines today this can mean things like your flows keep collecting in the background when you're not expecting it, still performing potentially expensive work
1
the
with*
methods explicitly replace them with safer alternatives, alongside
repeatOnLifecycle
1
c
218 Views