Refreshing SwiftUI on changes within objects in a collection.
How to approach updating UI for changes within objects held within a collection? After experimenting a bit, I think there are basically two approaches.
- The conformance to
BindableObjectby the object that contains the collection needs to be deep and not shallow. It needs to fire didChange if the collection, or anything recursively within the collection, changes. In my example from yesterday, this means that
MainViewModelwould need to figure out how to fire didChange if anything within any of its existing rows changes - not just changes to the collection itself. or
- The parent/container can have shallow conformance to
BindableObjectif the row model themselves conform to
BindableObjectand the row view declares the dependency with
You must do one or the other if you want a row to refresh when
isTyping changes value. I suspect that in the general case, #2 will be simpler.