In KVO I subscribe to receive change notifications from a potential sender with:
[potentialSender addObserver: self
forKeyPath: NSStringFromSelector(@selector(aProperty))
options: 0
context: myKVOHandle];
And I can unsubscribe with:
[potentialSender removeObserver: self
forKeyPath: NSStringFromSelector(@selector(aProperty))
context: myKVOHandle];
Where KVO handles might be created with this c idiom.
Are calls like these counted – so every add
must be directly paired to and precede a remove
– this is like the pre-ARC retain
and release
?
Alternatively, are they idempotent – any number of add
calls can be stopped by a single remove
, several remove
operations without a matching add
are safe?
I believe the idempotent approach is used for [aControl addTarget:action:forControlEvents:]
and also . I was wrong about notification centre [aNotificationCentre addObserver:selector:name:object:]
addObserver
calls. They are not idempotent. However, removes are. Thanks to Rob for correcting me on this.