Travis Griggs
04/10/2024, 9:34 PMvar base by remember { mutableIntStateOf(10) }
var a by remember(base) { mutableIntStateOf(base * 2) }
var b by remember(base) { mutableIntStateOf(base * 3) }
val c = remember(a, b) { (a * b) }
val d1 by remember { derivedStateOf { (a * b) } }
val d2 by remember(base) { derivedStateOf { (a * b) } }
I've read and recognize that c would be the better case for the product of a and b. I wanted to play with the derived case because sometimes nested mutable states makes the derived state attractive because of its ability to "auto observe" the less direct references.
In the above example, c, d1, and d2 update fine as long as only a and b are change. But if new mutable states are created because the base is changed, then new a/b mutableStates are created. d2 continues to work as expected. And d1 does not. Which I understand (if you skip the by
delegates, and type out all of the .value extensions for a and b, it's a little clearer why in my opinion). Because the original derivedState references the original mutableStates, it has no ability to know that it needs to be updated. It's auto observed values are stale. Here's what surprised me though. If I change that d1 line to this:
val d1 by remember { derivedStateOf {
println("base reference $base")
(a * b) } }
That didn't work either. I can see that it's indeed rerunning the computation when base changes. But it's fetches of a and b don't seem to update. Why is that?Ben Trengrove [G]
04/10/2024, 11:45 PMBen Trengrove [G]
04/10/2024, 11:46 PMTravis Griggs
04/11/2024, 12:26 AMclass Person {
var name by mutableStateOf<String>("foo)
}
var persons by mutableStateOf(listOf(... multiplePerson objects ...))
and then had a list ui for them where I had
val sortedPersons by remember {
derivedStateOf {
persons.sortedBy { each -> each.name } } }
And I could add/remove new people from the list (as long as I updated the persons with the modified list) OR I could change their names, and the sorting worked fine. IIRC, I did not have to use any remember keys for that to work correctly. So in that case, as persons were added, it seemed to update the nested observation of the names. Did I confuse myself?Travis Griggs
04/11/2024, 12:27 AMBen Trengrove [G]
04/11/2024, 3:30 AMTravis Griggs
04/11/2024, 3:47 AMBen Trengrove [G]
04/11/2024, 4:33 AMshikasd
04/11/2024, 3:28 PMby
for a
and b
and then printing out states directly.
You'll see that:
• d1 captures MutableState(...)@1
and MutableState(...)@2
,
• when you change base
, it creates MutableState(...)@3
and MutableState(...)@4
, which is read by d2
, but never read in d1
, as the calculation
lambda you provided to the derived state never updated.Zach Klippenstein (he/him) [MOD]
04/12/2024, 6:13 PMZach Klippenstein (he/him) [MOD]
04/12/2024, 6:14 PMshikasd
04/12/2024, 6:16 PMZach Klippenstein (he/him) [MOD]
04/12/2024, 6:23 PMBen Trengrove [G]
04/12/2024, 6:25 PMshikasd
04/12/2024, 6:29 PMZach Klippenstein (he/him) [MOD]
04/12/2024, 6:30 PMZach Klippenstein (he/him) [MOD]
04/12/2024, 6:31 PM