I've got a simple data model that includes a PassthroughSubject like so:
final class PeopleDataModel: NSObject, ObservableObject {
var didChange = PassthroughSubject<Void, Never>()
private lazy var fetchedResultsController: NSFetchedResultsController<Person> = {
...
}()
public var people: [Person] {
return fetchedResultsController.fetchedObjects ?? []
}
}
extension PeopleDataModel: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
people.forEach { print($0) }
didChange.send()
}
}
So my intent is when Core Data is updated, it'll send the message out. Now I try and use it in my SwiftUI view:
struct ContentView : View {
@ObservedObject var peopleDataModel = PeopleDataModel()
var body: some View {
NavigationView {
List(peopleDataModel.people) { person in
NavigationLink(destination: PersonDetailView(person: person)) {
PersonRow(person: person)
}
}
.navigationBarTitle(Text("People"))
}
}
}
The view doesn't actually update with the people that have been inserted into Core Data. They show properly in the print from the data model, but don't appear in the List. How do I actually "tie" these things together so that when Core Data updates the List knows that it needs to redraw itself?