I want to observe recomposition using prints, but ...
# compose
m
I want to observe recomposition using prints, but it seems they are not perfectly reliable. If I use a print in a body, it is sometimes not called even if this function got recomposed. For instance here,
ChatTopBar
gets recomposed unnecessarily because
List
is not stable, and I can see in Layout Inspector that it gets recomposed, but this print is only executed during the initial composition phase. Do you know what might be the reason why it works this way? I can provide a complete example.
Copy code
@Composable
fun ChatTopBar(messages: List<Message>) {
    println("ChatTopBar recomposition")
    TopAppBar(title = {
        println("TopAppBar title recomposition")
        Text("Messages ${messages.size}")
    })
}
1
K 1
s
Each composable may have many different recomposition scopes, and each may be recomposing in different frequencies than others. If your print lives in a recomposition scope which simply gets skipped, it will not print anything.
m
Not sure what you mean. Even if they have different scope, if
ChatTopBar
gets recomposed, it should execute its body, so execute
print
, or am I wrong?
s
Yeah but
println("ChatTopBar recomposition")
and
println("TopAppBar title recomposition")
live in different recomposition scopes, provided TopAppBar is not
inline
m
My problem is that I know
ChatTopBar
should get recomposed due to
List
not being stable (it hasn't changed, but it is unstable, so it should trigger recomposition), and I can see that it gets recomposed in Layout Manager, but
print
does not seem to be called.
Yes, but they both print to the same console, no?
s
Do you have strong skipping enabled? It may be skipping List anyway if it's equal referentially
m
This is a new project and I assumed it is not set by default, I will check it now
s
It is on by default in the latest compose versions
m
o.0
Thanks for letting me know, I missed that, and that is important
Yes, this was the problem, when I explicitly disabled it, now everything gets recomposed and prints work as I expected
👍 1
Documentation should be updated
😂 1
s
p
Also, if you are sure it recomposed because the layout inspector but you don't see the printed text. Then you might be a victim of " doughnut hole skipping" https://www.jetpackcompose.app/articles/donut-hole-skipping-in-jetpack-compose
s
If layout inspector is showing this function as recomposed when it actually didn't, it sounds like a bug
487 Views