PDA

View Full Version : coredata observing changes




MrFusion
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
forKeyPath:@"title"
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld)
context:[self managedObjectContext]];
}


-(void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
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
ofObject:object
change:change
context:context];
}
}
@end


output:
[/CODE]
observe
*** -[NSCFString userInfo]: unrecognized selector sent to instance 0x10770
*** -[NSCFString userInfo]: unrecognized selector sent to instance 0x10770
[/CODE]

What is the best way to proceed here? Thanks!