Receiving 2 KVO notifications for a single KVC change

后端 未结 2 1180
花落未央
花落未央 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: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]; }

提交回复
热议问题