Receiving 2 KVO notifications for a single KVC change

后端 未结 2 1179
花落未央
花落未央 2020-12-16 13:36

I\'m using KVC/KVO to create a custom bindings implementation for a University project (it needs to be custom as I want to do things beyond what bindings can do, including r

相关标签:
2条回答
  • 2020-12-16 13:40

    Perhaps put an NSLog in your addObserver call, to see if you're adding two observers.

    0 讨论(0)
  • 2020-12-16 13:49

    You're doing this:

    - (void)setName:(NSString *)name
    {
        [self willChangeValueForKey:@"name"];
        [contact setFirstName:name];
        [self didChangeValueForKey:@"name"];
    }
    

    But (by the sounds of it) from a non-NSManagedObject subclass. This means that Cocoa will be attempting to send KVO notifications automatically for you. You're supplementing that by sending your own too. Solutions:

    • Override +automaticallyNotifiesObserversForKey: to return NO
    • Change your method to:

      - (void)setName:(NSString *)name { [contact setFirstName:name]; }

    0 讨论(0)
提交回复
热议问题