[SOLVED] How to observe every child entity
[SOLVED] How to observe every child entity
- Subject: [SOLVED] How to observe every child entity
- From: Matthew Lindfield Seager <email@hidden>
- Date: Tue, 15 Sep 2009 21:25:10 +1000
The following is what I ended up with and it seems to work. Please
feel free to pimp my code though... not only do I still have a LOT to
learn but I also really struggle with knowing when to release and
retain.
I plan to change it to use a context but figuring out the best
practice for that can wait for another day!!!
For what it's worth this also answers the "KVO / Core data question"
thread that Paul Gribble started in December 2005 but that wasn't
resolved [on list?].
// Account.m
- (NSDecimalNumber *)calcBalance {
return [self valueForKeyPath:@"email@hiddent"];
}
+ (NSSet *)keyPathsForValuesAffectingCalcBalance {
return [NSSet setWithObjects:@"transactions", nil];
//should I be returning the super's set as well?
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if ( [keyPath isEqual:@"amount"] ) {
[self willChangeValueForKey:@"calcBalance"];
NSLog(@"pretending to change calcBalance so observers will know to
get it again");
[self didChangeValueForKey:@"calcBalance"];
} else {
NSLog(@"Different path");
[super observeValueForKeyPath:keyPath ofObject:object change:change
context:context];
}
}
// Transaction.m
- (Account *)account {
return account;
}
- (void)setAccount:(Account *)newAccount {
if ( newAccount == account )
return;
[self willChangeValueForKey:@"account"];
if ( account != NULL )
[self removeObserver:account forKeyPath:@"amount"];
[account release];
account = [newAccount retain];
if ( account != NULL ) // not really necessary as we can send messages to NULL
[self addObserver:account forKeyPath:@"amount" options:0 context:nil];
[self didChangeValueForKey:@"account"];
}
- (void)awakeFromFetch {
[self addObserver:account forKeyPath:@"amount" options:0 context:nil];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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