Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data: Emitting KVO notifications for transient, derived properties
    primarykey
    data
    text
    <p>I have <code>Parent</code> entity with a custom class that has a transient and derived (read only) property called <code>DerivedProperty</code>.</p> <p>The value of <code>DerivedProperty</code> is dependent on the value of <code>Parent.IndependentProperty1</code> and so whenever <code>IndependentProperty1</code> changes, the value of <code>DerivedProperty</code> will change. However, <code>Parent</code> has a to-many relationship to <code>Child</code> (called <code>children</code>) and <code>DerivedProperty</code> is also dependent on the value of <code>IndependentProperty2</code> in all of <code>Parent</code>'s <code>Child</code> objects.</p> <p>So whenever <code>IndependentProperty1</code> of <code>Parent</code> or <code>IndependentProperty2</code> of any of the <code>Child</code> objects changes, I would like to notify any observers that <code>DerivedProperty</code> has also changed.</p> <p>I've arrived at the following code so far. The only problem is that no KVO notifications are emitted for <code>DerivedProperty</code> since if I try to do this in <code>objectContextDidChange:</code> then the code will end up in a loop.</p> <pre><code>- (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 ; } </code></pre> <p>I'm sure I need a total rethink on my approach here. I've tried using KVO to observe <code>IndependentProperty1</code> and <code>IndependentProperty2</code> 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 <code>keyPathsForValuesAffectingValueForKey:</code> but of course that won't work across a to-many relationship.</p> <p>How to I get KVO notifications working with a Core Data transient, derived property that is dependent on a to-many relationship?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload