I found the lambda containing CoroutineScope is un...
# compose
a
I found the lambda containing CoroutineScope is unstable as CoroutineScope is unstable,
Copy code
val scope=rememberCoroutineScope()
Text(text="text", onClick={
    scope.launch{...}  //cause recomposition every time
}
This situation also applies to lambdas containing ViewModel. but warpping every lambda containing unstable variables with
remember{}
is inconvenient. Is there a better way to handle it?
3
l
I don't know why these lambdas don't get remembered properly like others. Isn't a compiler-generated
remember(coroutineScope) { { coroutineScope.launch { /* ... */ } } }
enough?
e
@Loney Chou the issue is that the
onClick
lambda captures the
CoroutineScope
and gets compiled into a unstable class because it has a
CoroutineScope
property - https://multithreaded.stitchfix.com/blog/2022/08/05/jetpack-compose-recomposition/#gotcha---unstable-lambdas
l
I know that, but the fact that we have to wrap
CoroutineScope
into a
@Stable
form is quite annoying. And if you say so, then it is of no use to remember that lambda, because its actual type is still unstable, thus causing recomposition as well.
e
Remembering it should work since it will then pass the equality check
l
I don't know why these lambdas don't get remembered properly like others. Isn't a compiler-generated remember(coroutineScope) { { coroutineScope.launch { /* ... */ } } } enough?
Then why? 😇 I mean, if you create another lambda instance, the stability won't magically change, so there's no point to not
remember
that.
174 Views