I have encountered the following problem with a List.
The setup is as follows
@State private var allItems : [SomeItem]
@State private var selected : SomeItem?
// in the body
List(allItems, $selection) { theItem in …
}
where SomeItem is a struct.
When some properties of an item in allItems changes, the values that I read in theItem are not updated.
Just as if old content was cached.
I changed allItems to a computed var and everything works OK.
I read this SO thread https://stackoverflow.com/questions/74083515/swiftui-list-item-not-updated-if-model-is-wrapped-in-state but that does not give a full explanation.
So my questions:
- Is there effectively an issue here ?
- when is it safe to use a State var as the Content of List ?
- Is it OK (it seems) if the properties of items do not change ?
- It seems also OK if the list of allItems is modified (appended, reduced) without problem changing the properties of its elements.
- How to do if needed to change both allItems (append for instance) and change the properties of some items, as computed var cannot be modified.
Hope the question is clear.