Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data 'modified' timestamp

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!

like image 362
Nathan Gaskin Avatar asked Jun 04 '26 10:06

Nathan Gaskin


1 Answers

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,

like image 71
Olivier Tabone Avatar answered Jun 06 '26 03:06

Olivier Tabone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!