@ObservableObject and NSManagedObject

Can I use NSManagedObject with @ObservableObject and @Published to auto-reconfigure my collection view's cells?

Answered by Zimmie in 716922022

Yes, though I would drive a collection view with an NSFetchedResultsController and bindings.

NSManagedObject subclasses are automatically observable. You don't need to do anything for ObservableObject conformance, nor do you need to annotate properties with @Published. In SwiftUI views, you can just add an @ObservedObject var for your managed object, and the view tracks as properties and relationships of that object update.

Accepted Answer

Yes, though I would drive a collection view with an NSFetchedResultsController and bindings.

NSManagedObject subclasses are automatically observable. You don't need to do anything for ObservableObject conformance, nor do you need to annotate properties with @Published. In SwiftUI views, you can just add an @ObservedObject var for your managed object, and the view tracks as properties and relationships of that object update.

@ObservableObject and NSManagedObject
 
 
Q