please consider the following swiftUI scenario:
List {
ForEach(manager.items(filter: .all), id: \.id) { item in
RowView(item: item)
}
}
where manager
is an environment object injected via @EnvironmentObject var manager: Manager
. The items()
method returns an array of core data entities fetched depending on the filter parameter. This works well for mutating elements, but whenever an element is added or removed, the ForEach view is not notified (of course, because its a function return and no "binding" or such).
Whet is the bes practice approach to update the list accordingly? Can I somehow wrap the function call? Or notify about changes? Any tip is appreciated!
I'm not sure how this behaves with core data fetching and how well it performs, but you can try to bind your fetch results like this:
Or:
With Swift 5.3, referencing with
self.
can be omitted.