Work outside composition.
WHY SPECIAL HANDLING IS NEEDED
Composables may run many times, in any order, so ordinary code placed in them runs unpredictably.
WHAT THE EFFECT FACILITIES PROVIDE
Running work when a composable enters composition, keyed to values Running work after every composition Cleaning up when a composable leaves
WHAT TO USE FOR STARTING SOMETHING WHEN A SCREEN APPEARS
The effect that launches a coroutine, keyed appropriately.
WHAT THE KEY DETERMINES
When the effect restarts.
WHAT GOES WRONG
A constant key, so it never restarts when it should A changing key, so it restarts constantly
WHAT TO ALWAYS PROVIDE
Cleanup: cancelling subscriptions, releasing resources.
WHAT TO AVOID
Launching network requests directly from composition Holding a reference that outlives the composable
WHAT TO PREFER
Doing work in the view model, and observing the result.
WHY
It survives configuration changes and is testable.
WHAT TO BE CAREFUL WITH
Callbacks capturing stale values.
WHAT TO TEST
Behaviour on rotation, and on rapid navigation away and back.