Observing related attributes via a to-many relationship
Observing related attributes via a to-many relationship
- Subject: Observing related attributes via a to-many relationship
- From: email@hidden
- Date: Sun, 20 May 2007 18:02:42 +0000 (GMT)
I am new to all this and am struggling to do something that is, I'm sure, really simple!
I have created a two-entity model with core data ('Master' and 'Detail') with one attribute in Master ('total') and one in Detail ('detailCharge'). I have created reciprocated to-many relationship linking the two. I have created model objects for 'Master' and 'Detail' (subclassing NSManagedObject) and simply want the Master-total attribute always to equal the sum of related Detail-detailCharge attributes. This seems like a trivial job for Key Value Observing but I just can't manage it!
I am trying to observe the to-many relationship from the Master class and have tapped into the auto-generated accessor methods with observer regstration as follows:
- (void)addToDetailObject:(NSManagedObject *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"toDetail" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey: @"toDetail"] addObject: value];
[self didChangeValueForKey:@"toDetail" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
// This is the new bit.
[value addObserver:self
forKeyPath:@"toDetail.converted"
options:NSKeyValueObservingOptionNew
context:NULL];
[changedObjects release];
}
I've created a corresponding removeObserver: method within the removeToDetailObject method. I've also added an observeValueForKeyPath method to 'Master'. None of this works. I can't see the observer registration method being called when new Detail objects are added. I know I've done something stupid here. Could anyone help?
Many thanks
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden