I'm a bit confused about KVO in Objective-C. If my property is KVO-compliant, do I have two choices to emit the notification: 1. addObserver:.../observeValueForKeyPath:...
and 2.did/willChangeValueForKey...
, or should we use the methods did/willChangeValueForKey
in a particular context?
I noticed that the second one (did...
) can be used to group a number of changes into a single notification; are there other important things to understand about those two ways? Is the first method (addObserver:...
) impossible to use in some contexts (although the context would be KVO-compliant)?
edit:
in a subclass of NSManagedObject, only did/willChangeValueForKey...
were used, but not addObserver:.../observeValueForKeyPath:...
: do you know why?
Those two pairs are used by the two sides to the KVO interaction; they are not at all interchangeable.
addObserver:forKeyPath:options:context:
and observeValueForKeyPath:ofObject:change:context:
are used by the client, the one who wants to do the observing.
observeValueForKeyPath:ofObject:change:context:
is essentially a callback; it will be sent by the internal KVO mechanisms when the observed ("provider") object uses the other set of methods (the various did
/will
changeValueFor...
). The notification is actually made at that time, by the change
methods.