Ayla
05/24/2023, 5:19 AMval 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?Loney Chou
05/24/2023, 7:23 AMremember(coroutineScope) { { coroutineScope.launch { /* ... */ } } }
enough?eygraber
05/24/2023, 8:39 PMonClick
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-lambdasLoney Chou
05/25/2023, 12:12 AMCoroutineScope
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.eygraber
05/25/2023, 12:26 AMLoney Chou
05/25/2023, 12:31 AMI 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.