Why no willChange:valuesAtIndexes:forKey: when mutating array objects?
Why no willChange:valuesAtIndexes:forKey: when mutating array objects?
- Subject: Why no willChange:valuesAtIndexes:forKey: when mutating array objects?
- From: Allan Odgaard <email@hidden>
- Date: Thu, 26 Feb 2004 13:12:52 +0100
When an array controller's content is bound to another object, is there
any reason the array controller will not send a
willChange:valuesAtIndexes:forKey: to that object, when an object in
the array receives a setValue:forKey:?
I assume one problem is that several array controllers could be bound
to the same key, and so multiple notifications would be sent -- and
technically, it is not the array controller which mutate the object (it
is e.g. the NSTableColumn), so it really ought to be the mutated object
itself who send the willChangeValueForKey: message, which should then
be picked up by the array (and be re-broadcasted as
willChange:valuesAtIndexes:forKey:) -- but for this to work, the array
would need to observe all keys in the object, which is probably not
practical -- or there could be some sort of cooperation, i.e. having
the array provide virtual keys which, when observed, causes it to
observe that key in each of the stored objects (similar to the
arrangedObjects "proxy" of the NSArrayController).
To exemplify my "problem" with the current behaviour:
Imagine we write a simple reminder program. Our model has an array of
NSDictionary objects with a key for "message" (NSString) and a key for
"time" (NSDate) -- i.e. the array really is the model.
A method will setup a timer to trigger, when the next event should
appear.
If we want to edit the events then we bring up a table view with a
controller with the event array as content (and use an NSDateFormatter
in the table view for the time column).
This is all very simple, and we can make the model observe its own
array, that way it will learn when new events are added (or old ones
are deleted), and reset the timer.
But what when an existing event's time is changed? there is no way for
the model to learn about that, unless it observes every object in the
event array, but this array is "dynamic", so it would have to
add/remove itself as observer each time an event is added/removed -- so
instead of providing the array as-is, we must implement the indexed
accessors (to know when the array change) and add/remove our model as
observer for objects (as they are added/removed/replaced).
I realize that the model could bind to the array controller's
"arrangedObjects.time", but that would a) require that the array
controller is always present (the reminder program would not load the
GUI (and thus create the controller), before the user actually requests
to edit events).
Also, binding to arrangedObjects seems ineffective, as it will pass the
entire array as "changed", w/o providing an NSIndexSet to pinpoint the
exact changes.
Comments?
_______________________________________________
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.