Composition is a transaction. Anything you do in a
@Composable
function shouldn't have observable side effects until after that transaction commits successfully.
SideEffect {}
does this, deferring its block of code until the composition transaction that scheduled it is successful.
👍 1
Adam Powell
08/29/2021, 6:35 PM
SideEffect
is also guaranteed to run that code on the applier thread (e.g. the Android UI thread) whereas composition itself may run on a background thread in the future
Adam Powell
08/29/2021, 6:36 PM
If composition fails, then nothing in that composition transaction is meant to have ever happened. By using
SideEffect
you can meet this contract. Otherwise you would need to ensure yourself that no mutations you make are visible externally before composition succeeds.
f
Florian
08/29/2021, 6:44 PM
So the difference is that without
SideEffect
the call to
setSytemBarsColor
would execute if the Composable was canceled for some reason?
Florian
08/29/2021, 6:44 PM
but in reality it would probably not make any observable difference, is that correct?
a
Adam Powell
08/29/2021, 9:22 PM
Writing wrong code that works by luck will generally come back to bite you eventually 🙂
☝️ 1
f
Florian
08/30/2021, 8:12 AM
@Adam Powell of course, I'm just asking this for understanding