Tuesday, January 13, 2015

Dependent Key Paths

Jonathon Mah:

If user defaults can change separately from this code (and you should probably assume it can), providing correct KVO change notifications can be accomplished by having the singleton observe NSUserDefaults with NSKeyValueObservingOptionPrior; posting willChangeValueForKey:@"mySetting" on the prior callback and didChangeValueForKey:@"mySetting" on the post callback. But this sucks — it’s wordy and you pay a (small) performance cost even if mySetting isn’t observed.

[…]

It’s valid for dependencies to be specified in either the dispatching or specific methods, all the way up the inheritance chain. Correctly overriding +keyPathsForValuesAffectingValueForKey: requires some tricky code, which I’ve wrapped in a function MLHOverrideKeyPathsForValueAffectingKey in MyLilKeyPathHelpers.

Comments RSS · Twitter

Leave a Comment