Hey. Is it just wrong to use Launched Effect when ...
# compose
v
Hey. Is it just wrong to use Launched Effect when you don't need the block to be suspended? Like common use case:
Copy code
LaunchedEffect(navigationRequest) {
   navigator.pushNotSuspend(navigiationRequest)
   // perhaps notify vm that request consumed, but that is another topic
}
Because Launched Effect needs additional time to launch a coroutine which we don't actually utilize? What to use instead? Just:
Copy code
remember(navigationRequest) {
    navigator.pushNotSuspend(navigiationRequest)
}
s
DisposableEffect(Unit) { doYourThing() onDispose {} }
That one does not launch a coroutine
v
Yea, I thought about that. Just was a little bit unsure to use dispose while not using onDispose
s
Yeah that's fine, people reach for this when they want to avoid launching a coroutine, exactly like you describe your use case here
v
Because I just realized that 95% of my LaunchedEffect blocks don't need suspension
And I noted that the first line inside the block of LaunchedEffect is called 200ms later than DisposableEffect's. But that is in debug. Dunno what would be the number in release
And I got the 🤦‍♂️ moment
j
I think you want
SideEffect
v
I still often need that thing to be called only once and not called on recompositions
SideEffect called on every succsefful recomposition
j
I would probably do a
snapshotFlow
of
navigationRequest
and perform navigation in the collection
👀 1
I'm reading the thread somewhat backwards...
v
But the collection is suspended. So we will need LaunchedEffect which will eat the time when we don't need it?
j
There is no real penalty for not suspending. Suspend functions can either return a result synchronously through the return value or suspend and propagate the result through the continuation.
Unless you're observing some kind of problem with it, I wouldn't worry about it.
Especially for something that only happens multiple times per minute, rather than like multiple times per millisecond
v
Copy code
2024-11-21 18:00:25.517 ---DisposableEffect()
2024-11-21 18:00:25.713 ----LaunchedEffect()
My current problem is that I see that my every navigate method is delayed by these almost 200ms (debug mode), when I never need that?!
j
Sounds like you have something monopolizing your main dispatcher and/or thread, as that's like 10+ frames later which is not normal.
🤔 1
v
Copy code
2024-11-21 18:59:49.168 ---DisposableEffect()
2024-11-21 18:59:49.179 ----LaunchedEffect()
In the release variant the difference is not that critical (10-50ms) but still if not needed then why.