I've got a curious condition in regards to recompo...
# compose-android
a
I've got a curious condition in regards to recomposition in Jetpack Compose for lazy lists. I assume that it's some kind of magic that Compose uses to determine what is and is not in scope, but it's still a bit confusing. I'm particular, it seems that, where
state
is a
MutableState
containing a list, and the list is being consumed in a Lazy container of some kind
Copy code
var x = state.value
items(x, ...) {
...
Does Not cause recomposition on every element in the list, while
Copy code
items(state.value, ...) {
...
Does force all items to recompose. In theory, moving
state.value
into its own local variable that is immediately referenced should not cause any difference, but it clearly appears to be the case here. What am I missing here? Is this just Compiler Magic that's seeping in?
s
If you've got a reproducer and more code around this it may help. This doesn't look correct no, as those two things should be equivalent afaik. This is kinda what this article talks about in the section "Expressions and function calls".
f
maybe it's related to this, though this ticket specifically mentions function capture https://issuetracker.google.com/issues/256100927
I woudl also note that you mention Lists, which are considered unstable by the Jetpack Compose compiler, if your state has lists, you should consider using Immutable Collections
a
I suspect that this is the likely answer. Alas!
Alright, good point. This is a mutablelist being exposed as a read only list, using immutable is probably a good idea too.
a
What I don't like about ImmutableList is right now there's no JSON converter for it, given that most lists in any app would be from a server response. Manually putting
toPersistentList
everywhere is annoying.
f
well, I would counter that by saying that your network models should not reach the UI. You should map your network models to some other model that the UI depends on, and in that map you can use immutable lists.
💯 2
in most cases, there are multiple maps, from data layer to domain layer to UI layer