I’m trying to set up an automatic ‘modified’ timestamp with my Core Data object graph. I’ve got it working within each model with the following code:
- (void)willSave
{
NSDate *date = [NSDate date];
[self setPrimitiveValue:date forKey:@"modified"];
[super willSave];
}
However, I’d like this modified date to be able to bubble up the object graph, changing the modified date for each parent object in turn.
So if I have: Grandparent–(hasMany)–>>Parent–(hasMany)–>>Child and I change a property of Child, the Parent and Grandparent modified timestamps should all update to the same value.
Is there a simple way to do this? Thanks!
You may register for the NSManagedObjectContextObjectsDidChangeNotification notification. In it’s userInfo, you get a list of updated, deleted, and inserted managed objects.
Next step would be to iterate over the inserted and updated objects, then introspect objects (isKindOfClass, respondsToSelector, …) and assign a timestamp on picked objects.
Best,