Re: KVO, design and documentaion
Re: KVO, design and documentaion
- Subject: Re: KVO, design and documentaion
- From: Bill Bumgarner <email@hidden>
- Date: Sat, 27 Jan 2007 20:20:34 -0800
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).
Have you registered 'observer' as an observer of the @"test"? keypath?
I suspect not. In such a case, the behavior of KVO appears to be
undefined.
I would bet that the crash from (1) is because the underlying KVO has
nothing in its internal bookkeeping tables related to the observer
and, thus, it ends up passing NULL to some random function -- CFRetain
(), perhaps -- that can't deal.
That (2) works is merely coincidental. The behavior is still
undefined (as far as I could find in the docs), but it happens not to
crash in this case.
Now, this fragility may come as a slight surprise. However, KVO is
designed to be really really fast and, as such, loading it up with a
boatload of assertions and validation code would just slow it down.
b.bum
_______________________________________________
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