--

There's a VERY subtle issue when using launchWhenStarted (or any of it's sibling functions) to observe flows that handle events/action. These functions use a pausing dispatcher that suspends on stop, and cancel on destroy. Thus it's possible to for launchWhenX to actually _drop_ a value when observing flows this way. (Plus you can actually receive a value outside of the "safe" lifecycle state depending on your use case.)

See my example here: https://themikeferguson.medium.com/pitfalls-of-observing-flows-in-launchwhenresumed-2ed9ffa8e26a

But don't take my word for it. Google's own article covers it too (though not as in-depth):

https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda

--

--

Michael Ferguson
Michael Ferguson

Written by Michael Ferguson

Android software developer. Views and opinions expressed are my own and not that of my employer.

No responses yet