While developing a universal app with the updated SwiftUI framework, I found that
@Published decorators do not function as intended in objects inheriting from
What’s exactly the expected behaviour anyway? We know that —
ObservableObjectprotocol can have variables marked with
@Published. SwiftUI views that depends on an observable object instance will update when a published variable receives a new value.
NSManagedObjectclass can have variables marked with
@NSManaged. SwiftUI views that depends on an NSManagedObject will update when the
@NSManagedvariable receives a new value in the managed context.
NSManagedObjectclass automatically conforms to
ObservableObject. So they should be able to mark variables with either
@NSManagedto have views updated automatically. You will want to use
@Publishedto update variables that update frequently but needs no disk persistence.
But here’s what is broken: Published variables in NSManagedObject instances do not update SwiftUI views as expected.
As discussed on Stack Overflow, this is likely a bug on Apple’s end. There have been discussions since September 2019 but this problem still exists today. Somehow the implementation of
ObservableObject protocol for
NSManagedObject doesn’t trigger
objectWillChange.send() method doesn’t get triggered, the workaround is to simply trigger it manually.