Even tho I read the documentation, I can't wrap my...
# compose
f
Even tho I read the documentation, I can't wrap my head around why we need to use
SideEffect
here. The functionality works without the
SideEffect
call. Can someone tell me why we need it? https://google.github.io/accompanist/systemuicontroller/
👍 1
a
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
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
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
So the difference is that without
SideEffect
the call to
setSytemBarsColor
would execute if the Composable was canceled for some reason?
but in reality it would probably not make any observable difference, is that correct?
a
Writing wrong code that works by luck will generally come back to bite you eventually 🙂
☝️ 1
f
@Adam Powell of course, I'm just asking this for understanding