View Full Version : coredata observing changes

Nov 3, 2009, 04:06 AM
Hello everyone

I have another design problem with my coredata program. The plan is to opensource this program, so your help won't go to waste. :)

I have a bunch of objects, each with a bunch of keys. When the value for these keys change, I have to update the value for another bunch of keys in different objects.
In previous versions, I added a postnotficiation method in every setter method for every key that I wanted to change (stil with me?).
But that seems a waste of code and time with objective-c 2.0 and @property - @dynamic.
So I have to observe the changes in another way.
NSManagedObjectContext handles all changes and updates the GUI, but overriding is strongly discouraged by the documentation.

The code below is another option I found in several places, but this is also giving problems.

@dynamic title;
-(void ) awakeFromInsert {
//new objects
[super awakeFromInsert];
[self addObserver:self
options:(NSKeyValueObservingOptionNew |
context:[self managedObjectContext]];

-(void) observeValueForKeyPath:(NSString *)keyPath
change:(NSDictionary *)change
context:(void *)context { NSLog(@"observe");
if (context == [self managedObjectContext]) {
if ([keyPath isEqual:@"title"]) {
[[NSNotificationCenter defaultCenter] postNotification:@"myUpdateNotification"];
// be sure to call the super implementation
// if the superclass implements it
[super observeValueForKeyPath:keyPath

*** -[NSCFString userInfo]: unrecognized selector sent to instance 0x10770
*** -[NSCFString userInfo]: unrecognized selector sent to instance 0x10770

What is the best way to proceed here? Thanks!