[PRO] KVO on NSMutableArray, a "one to many" fine point
[PRO] KVO on NSMutableArray, a "one to many" fine point
- Subject: [PRO] KVO on NSMutableArray, a "one to many" fine point
- From: DumberThanDanQuayle <email@hidden>
- Date: Sat, 17 Jan 2004 07:44:02 -0500
The purpose of this post is help clarify the KVO documentation and save
the community from wasting their time on what I thought was a KVO bug .
There are essentially two points here.
(1) When unregistering an object for KVO, using the "to one" method
[NSObject removeObserver: forKeyPath:] or the "to many" variant
[NSArray removeObserver: fromObjectsAtIndexes: forKeyPath:], Cocoa will
crash if you have not called corresponding addObserver method
previously.
Unlike the retain-release paradigm, there is no "balancing" of
redundant addObserver calls with some kind removeObserver count. More
clearly, you call can call the addObserver method repeatedly without
any problem, but it only takes one removeObserver call to erase that
history. There is nothing like a retain count in KVO.
That is easy enough to figure out. Now for the fine point.
(2) While the KVO effect is the same, the mechanisms underly the "to
one" and "to many" methods are fundamentally different. So Cocoa will
crash if try to mix "to one" methods with "to methods" in the context
of trying to balance addObserver calls with removObserver.
Thus, if you try to do this;
a. [NSMutableArray addObserver: toObjectsAtIndexes: forKeyPath:
options: context:],
b. [NSMutableArray addObject:addObject];
c. [addedObject addObserver: forKeyPath: options: context:]
d. [NSMutableArray removeObserver: fromObjectsAtIndexes: forKeyPath:]
You will have a problem at line d when "addedObject" is handled.
Again, this because KVO does not support freely mixing "to one"
methods with "to many" methods. Which variant is faster may depend on
the size of your arrays.
~ George
"[T]he best way to find these terrorists who hide in holes is to get
people coming forth to describe the location of the hole, is to give
clues and data."
George W. Bush
Washington, D.C., Dec. 15, 2003
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.