Eugeny Sentsov
07/15/2023, 10:35 AMderivedStateOf
. The documentation says
Derived states without mutation policy trigger updates on each dependency change.So I would basically expect
derivedStateOf
without policy parameter to behave like it behaves with neverEqualPolicy.
But a simple test (a bit modified version of the documentation example):
@Composable fun CountDisplay(count: State<Int>) {
println("Count: ${count.value}")
}
@Composable fun Example() {
var a by remember { mutableStateOf(0, neverEqualPolicy()) }
var b by remember { mutableStateOf(0, neverEqualPolicy()) }
val sum = remember { derivedStateOf { a + b } }
CountDisplay(sum)
}
shows that it’s not true - changing either a or b to the same value triggers the recalculation, but does not trigger CountDisplay
execution.
Could anyone help me to understand what I’m missing here?shikasd
07/15/2023, 11:05 AMshikasd
07/15/2023, 11:09 AMval sum by remember { derivedStateOf { a + b } }
val nested by remember { derivedStateOf { sum.also { println("recalculated nested" } } }
If you read nested state, you'll see a lot of printlnsshikasd
07/15/2023, 11:11 AMCountDisplay
won't be executed in any case however, as both states apply structural mutation policy by default when read in compositionshikasd
07/15/2023, 11:14 AMEugeny Sentsov
07/15/2023, 11:15 AMshikasd
07/15/2023, 11:16 AMshikasd
07/15/2023, 11:18 AMEugeny Sentsov
07/15/2023, 11:19 AMequals
method override. I expected the second calculation to be executed when and only when the result of the first one changes, so did not care about equality. As a result, recomposition was triggered all the time.shikasd
07/15/2023, 11:22 AMEugeny Sentsov
07/15/2023, 11:25 AMshikasd
07/15/2023, 11:28 AMEugeny Sentsov
07/15/2023, 11:33 AM