Observing changes in the properties of an NSManage

2019-07-31 14:36发布

问题:

In my application, I observe the properties of a managed object. A change may lead to adjustments in some of its other properties, so the managed object itself receives a message of a changed property. These changes happen through bindings that are set up in the Interface Builder.

I have the following method in the implementation of the managed object:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ( !processingChange )
    {
        processingChange = YES;

        *** DO STUFF TO THIS MANAGED OBJECT'S PROPERTIES ***

        [self.managedObjectContext processPendingChanges];

        processingChange = NO;
        return;
    }
}

The processingChange boolean is there to avoid an endless "notification loop", but it is not working as I expect (plus it looks like a real dirty hack).

There must be another way to do this. Any suggestions?

回答1:

use MOMs' setPrimitiveValue:forKey: it doesnt generate KVOs



回答2:

I think no need to send the notification "by hand", take a look: https://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177i

The observeValueForKeyPath:ofObject:change:context: method is automatically invoked when the value of an observed property is changed in a KVO-compliant manner, or if a key upon which it depends is changed.

Maybe this is even the mistake?