Observing changes in the properties of an NSManagedObject: how to avoid looping?

微笑、不失礼 提交于 2020-01-05 10:14:49

问题


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?



来源:https://stackoverflow.com/questions/16997558/observing-changes-in-the-properties-of-an-nsmanagedobject-how-to-avoid-looping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!