Re: KVO, design and documentaion
Re: KVO, design and documentaion
- Subject: Re: KVO, design and documentaion
- From: "The Karl Adam" <email@hidden>
- Date: Sat, 27 Jan 2007 18:27:48 -0800
> The other big thing about KVO is that all notifications are
> funneled thru a
> single bottleneck, whereas with true notifications you get to say what
> method should be called by a given notification. We had a big
> discussion of
> this back here:
>
> <http://www.cocoabuilder.com/archive/message/cocoa/2006/7/14/167622>
Interesting discussion, I hadn't turned that up during my search for
information. The callback design is a bit Javaesque...
Since Java's design is based partially in ObjC and Cocoa, that's not
at all surprising.
My own problems with KVO were caused by repeatedly calling
[receiver addObserver:self ...]
on the same receiver over time, resulting in more and more
notifications for each change, leading to severe performance
degradation. The KVO implementation could have been more intelligent
about this by ignoring duplicate registrations.
Not to seem harsh, but programmer error isn't the fault of the APIs,
You're asking for the API to guard against you doing something that
might be perfectly valid. You could be registering multiple times with
different contexts and different notification options, how is the API
supposed to decipher between intentional abuse and your misuse?
Also, in general you must be careful with registering and unregistering:
1)
[object removeObserver:observer forKeyPath:@"test"]; // CRASH!!!
[object addObserver:observer forKeyPath:@"prop" options:0 context:NULL];
2)
[object addObserver:observer forKeyPath:@"prop" options:0 context:NULL];
[object removeObserver:observer forKeyPath:@"test"]; // SAFE!!!
As long as 'object' remains registered for some other key path, using
removeObserver: is safe. (I guess you could abuse that knowledge).
What would make you think that removing an observer before registering
one would be wise? Though I do agree that normally in Cocoa that
should've resulted in a no-op.
The whole implementation of KVO seems a bit awkward, and as a result
its correct use requires more attention than I initially expected.
Where you see awkward, I see an API relying on common sense from the
user of that API.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden