Without having to use @FetchRequest
at the View level has any been able to successfully implement a CoreData Stack with observability in a model with the model being used at the View level. The view model sits between the View & Coredata Stack
CoreData Stack
ViewModel (ObservableObject)
SwiftUI View
hi,
your ViewModel simply needs to use an NSFetchedResultsController
to hook into Core Data, and keep the array of Core Data objects that you would ordinarily define in the View (using @FetchRequest
) as a @Published
property. whenever the resultsController fires, update the array property.
be sure your View keeps a reference to the ViewModel as an @ObservedObject
.
hope that helps,
DMG