So I've got an entity in Core Data, lets call it Parent defined as:
extension Parent {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Parent> {
return NSFetchRequest<Timer>(entityName: "Parent")
}
@NSManaged public var id: UUID
@NSManaged public var children: Children
}
And it has some children
public class Children: NSObject, NSCoding, ObservableObject {
@Published var children: [Child]
init(children: [Child] = [Child]()) {
self.cycles = cycles
}
// code for encoding/decoding ...
}
and child has some definition of a collection of UUID/Strings/Int and is a struct
So initially the Parents are displayed in a List View To insert a new parent you tap a plus button, new Parent is created and is persistent
There is another view to add children to the parent
So what's the problem? Whenever a new parent is created, it makes the context dirty and context.hasChanges() returns true and the context can be saved for persistence
Whenever the children are updated the context is not made dirty and context.hasChanges() returns false, therefore the updates are not saved
I think the problem is because of classes being reference types it does not look like Parent has changed as the children object is the same - but the data in it has changed. So how do I get the changes to save?
Short of deleting the Parent and remaking it when changes occur or updating a last updated variable in the Parent I'm not seeing how I get the context to realise there are changes