I have Parent
entity with a custom class that has a transient and derived (read only) property called DerivedProperty
.
The value of DerivedProperty
is dependent on the value of Parent.IndependentProperty1
and so whenever IndependentProperty1
changes, the value of DerivedProperty
will change. However, Parent
has a to-many relationship to Child
(called children
) and DerivedProperty
is also dependent on the value of IndependentProperty2
in all of Parent
's Child
objects.
So whenever IndependentProperty1
of Parent
or IndependentProperty2
of any of the Child
objects changes, I would like to notify any observers that DerivedProperty
has also changed.
I've arrived at the following code so far. The only problem is that no KVO notifications are emitted for DerivedProperty
since if I try to do this in objectContextDidChange:
then the code will end up in a loop.
- (void) awakeFromInsert { [super awakeFromInsert]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectContextDidChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:self.managedObjectContext]; } - (void) awakeFromFetch { [super awakeFromFetch]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectContextDidChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:self.managedObjectContext]; } - (void) objectContextDidChange: (NSNotification *) notification { NSSet *updatedObjects = [[notification userInfo] objectForKey:NSUpdatedObjectsKey]; if ([updatedObjects containsObject:self] || [updatedObjects intersectsSet:self.children]) { //clear caches _derivedProperty = nil; } } - (void) didTurnIntoFault { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (NSString *) DerivedProperty { if (_derivedProperty == nil) { _derivedProperty = [self someExpensiveCalculation]; } return _derivedProperty ; }
I'm sure I need a total rethink on my approach here. I've tried using KVO to observe IndependentProperty1
and IndependentProperty2
of the to-many relation but I just can't seem to get it working right. If the derived property wasn't dependent on a to-many relationship then I'm sure I could just use keyPathsForValuesAffectingValueForKey:
but of course that won't work across a to-many relationship.
How to I get KVO notifications working with a Core Data transient, derived property that is dependent on a to-many relationship?