elye
01/15/2022, 10:57 AMSideEffect
. I cannot create a case of to differentiate it’s use and without it’s use as per https://stackoverflow.com/questions/70720751/whats-the-different-using-sideeffect-and-not-using-it-in-jetpackcompose. Can someone enlighten? Thank you 🙏Big Chungus
01/15/2022, 11:44 AMelye
01/15/2022, 1:11 PMelye
01/15/2022, 1:12 PMFunkyMuse
01/15/2022, 2:01 PMFunkyMuse
01/15/2022, 2:01 PMBig Chungus
01/15/2022, 2:08 PMFunkyMuse
01/15/2022, 2:18 PMAlbert Chang
01/15/2022, 4:21 PMAlbert Chang
01/15/2022, 4:23 PMLaunchedEffect
and DisposableEffect
. They are also side effects but the op is asking about SideEffect
function here.Adam Powell
01/15/2022, 4:43 PMAdam Powell
01/15/2022, 4:46 PMSideEffect {}
schedules the block of code you give it to run when the composition changes are applied. It's an output of the composition process but not directly to the composition's tree.Adam Powell
01/15/2022, 4:47 PMSideEffect
to recompose.elye
01/16/2022, 2:12 AMIt's an output of the composition process but not directly to the composition's tree.
Can you provide an example of how to make this happen?
if something inside there changes
I’m just trying to explore some tangible visible different between the two (With and Without SideEffect Block). I think if I can find how to trigger that something
to change
, I can see the different. The question is, what is that something
, and how to make it change
.elye
01/16/2022, 3:01 AM@Composable
fun TrySideEffect() {
var timer by remember { mutableStateOf(0) }
Box(contentAlignment = Alignment.Center) {
Text("Time $timer")
}
Thread.sleep(1000)
timer++
}
and
@Composable
fun TrySideEffect() {
var timer by remember { mutableStateOf(0) }
Box(contentAlignment = Alignment.Center) {
Text("Time $timer")
}
SideEffect {
Thread.sleep(1000)
timer++
}
}
Both will behave differently.
Don’t think it’s the best way to explain SideEffect
, but at least that’s one way to differentiate with without SideEffect
.
Also share my inputs in https://stackoverflow.com/a/70727052/3286489Adam Powell
01/16/2022, 3:18 PMThread.sleep
in composition or in a SideEffect
is always wrongAdam Powell
01/16/2022, 3:20 PMelye
01/18/2022, 1:33 AMSideEffect
.
The Thread.sleep
is just used to represent busy logic. So if one has a busy logic, it should never be placed in the composition function at all. At most, it can be placed in SideEffect
.
The above example is not the best way to explain the use of SideEffect
, but it’s the simplest way I can think of to differentiate them without adding too many other things that makes understanding it complicated. Sorry, my brain is limited in digesting concept, making the simplest thing possible to help make sense out of things.Adam Powell
01/18/2022, 1:46 AMSideEffect
either. It will block the main thread before layout and drawing are permitted to proceed for the current frame.elye
01/18/2022, 3:52 AMLaunchEffect
or SideEffect
or some other approach?Adam Powell
01/19/2022, 2:51 AMLaunchedEffect
. It's something that happens over time, which implies suspend
, and you're describing it as a noun - "a [...] timer" - which implies that it has presence in the composition, it's not just a change that you apply to some other object.Adam Powell
01/19/2022, 2:52 AMSideEffect
are pretty narrow. If you're trying to use composition to update some other object that isn't otherwise related to compose in any way, but creating a subcomposition with a custom applier would be overkill, it comes in handy.