Anyone else find the naming of `lifecycleScope.lau...
# android
o
Anyone else find the naming of
lifecycleScope.launchWhenStarted {}
misleading?
lifecycleScope.launch { ... }
-> launches the coroutine immediatly, gets canceled when lifecycle is destroyed. Now I would expect
lifecycleScope.launchWhenStarted { ... }
to behave the same as the above, except it being different in the timing of the coroutine launching: coroutine gets launched when the lifecycle is started, and gets canceled when lifecycle is destroyed. But what it actually does, is also suspend the coroutine after it is launched, when the lifecycle is stopped. Maybe I've misunderstood it, but if not, I think a more fitting name would be something like
lifecycleScope.runWhileStarted {...}
? Or maybe perhaps a whole new scope? (
lifecycleStartedScope
), It feels like these
launchWhen
methods try to emulate sub-coroutinescopes that could be replaced by introducing new lifecyclescopes that are different instances of CoroutineScope, an API that exists for handling the lifecycles of coroutines. Curious to hear your thoughts about this.
👍 5
e
I don't find it as confusing in
Lifecycle.whenStarted(...)
, but I do agree about
LifecycleCoroutineScope
method naming
a
I agree with OP, I argued for the described expected behavior and lost 🙂 to this day I still use my own variants that behave closer to that way instead
the pausing dispatcher causes bugs and holds onto memory for much longer than it should, since cancelled coroutines can't resume with their CancellationExceptions until the associated lifecycle becomes started again
the extensions I use instead launch on the described "up" lifecycle event, cancel on the respective "down" event (e.g. start/stop are paired) and re-launch when the associated up event is encountered again
👍 5
d
Could you maybe make a gist of them if they're shareable @Adam Powell? It could help a bunch of us 🙂 !
f
j
missing dispatcher also brings problem that we can't use Flow.launchIn.
a
o
Good discussion guys. Side question: In a Fragment , is it better to call the
lifescopeScope.launchWhenStarted {}
/
.addLiveLauncher
methods in
onCreate
or
onViewCreated
?