https://kotlinlang.org logo
Title
c

Colton Idle

04/17/2023, 12:37 PM
If I have an expensive calculation that I need to run in a composable, then I almost always instinctively reach for
remember
. But wouldn't a launched effect also serve a similar purpose if I only want something to recompute if x changes? i.e.
val someValue = remember(x) { someHeavyCalculation() }
vs something like
val someValue = null

LaunchedEffect(x){
  someValue = someHeavyCalculation()
}
m

myanmarking

04/17/2023, 12:43 PM
Does launched effect runs right away in the same frame, or just in the next? Not sure
I think the default dispatcher is .immediate so it might work. But relying on a specification like that is not good i think
f

Filip Wiesner

04/17/2023, 12:50 PM
someValue
would have to be
mutableStateOf
for this to work (apart from it being
val
instead of
var
)
Does launched effect runs right away in the same frame
Correct me if I'm wrong but I think that
LaunchedEffect
and all other effects run after successful composition so all changes made in any effect would be only visible in next frame.
m

myanmarking

04/17/2023, 12:55 PM
Thats my doubt as well. You might be right
f

Filip Wiesner

04/17/2023, 1:00 PM
But maybe the "_effects run after successful composition_" part applies only to
SideEffect
. I am actually not sure now 😅
v

vide

04/17/2023, 1:15 PM
A compose dev will have better insight but I think it applies exclusively to
SideEffect
. I think
LaunchedEffect
runs interleaved with the composition 🤔
At least trying to call a focus requester from a
LaunchedEffect
will probably fail as its not in the tree yet
s

Stylianos Gakis

04/17/2023, 2:08 PM
No I am fairly certain that the LaunchedEffect lambda runs after the first successful composition. Which is why you sometimes see people using DisposableEffect with an empty
onDispose
because they want this to run instantly and not with 1 frame delay. Relevant thread here https://kotlinlang.slack.com/archives/CJLTWPH7S/p1622560036078600?thread_ts=1622556885.065600&cid=CJLTWPH7S and here https://kotlinlang.slack.com/archives/CJLTWPH7S/p1674253277230959?thread_ts=1673947731.751509&cid=CJLTWPH7S and here https://kotlinlang.slack.com/archives/CJLTWPH7S/p1650034947332099?thread_ts=1650031631.129979&cid=CJLTWPH7S
v

vide

04/17/2023, 2:47 PM
TIL! Thanks for the clarification
As a tangential discussion, any idea why calling focusrequesters fails from a
LaunchedEffect
? I would imagine the nodes are registered in the tree by the time the previous frame is done