Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Disable automatic KVO notications for ...Value setters #107

Open
smic opened this Issue · 2 comments

2 participants

@smic

If you observe the ...Value properties you will get two notifications if you set the value. One by the the automatic notification and one by the keyPathsForValuesAffectingValueForX method.

I know that observing the ...Value properties doesn't make sense. Performance-wise it will be much better to observe the original property. But it might impact the performance if the automatic notification is enabled.

  • (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
    NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];

    if ([key isEqualToString:@"blaValue"]) {
    NSSet *affectingKey = [NSSet setWithObject:@"bla"];
    keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey];
    }

    return keyPaths;
    }

  • (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
    if ([key isEqualToString:@"blaValue"]) {
    return NO;
    }

    return [super automaticallyNotifiesObserversForKey:key];
    }

@rentzsch
Owner

I'm confused:

I know that observing the ...Value properties doesn't make sense.

Agreed. I don't see folks doing it. So what's the problem? You're worried the generated keyPathsForValuesAffectingValueForKey: is somehow slowing down KVO in general?

@smic

I had a dependency in one of my properties via keyPathsForValuesAffectingValueForKey: . In this case it shouldn't matter if you use "propertyA" oder "propertyAValue", but I noticed that I get two KVO notifications.

-- (id)myProp {
return [NSNumber numberWithFloat:self.managedObject.propertyAValue + 10.0f];
}

++ (NSSet *)keyPathsForValuesAffectingMyProp {
return [NSSet setWithObject:@"managedObject.propertyAValue"];
}

If you depend on two properties you get four notifications and so on.
I want only prevent that others fall into the same trap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.