coredata observing changes

Discussion in 'Mac Programming' started by MrFusion, Nov 3, 2009.

  1. macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    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.
    Code:
    @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!
     

Share This Page