Sending NSKeyValueChangeSetting automatically for to-many keys
Sending NSKeyValueChangeSetting automatically for to-many keys
- Subject: Sending NSKeyValueChangeSetting automatically for to-many keys
- From: Allan Odgaard <email@hidden>
- Date: Mon, 5 Jan 2004 18:29:26 +0100
Hi there,
I have a to-many key in my model and I implement the to-many accessor
methods (countOf<key>, objectIn<key>AtIndex etc.). That way, if
somebody observes this key, they will get the various
NSKeyValueChangeInsertion, NSKeyValueChangeRemoval and
NSKeyValueChangeReplacement change notifications, if the collection
change.
But if a key of an object in the collection is changed, no notification
is sent.
E.g. id obj = [model objectsInAccountsAtIndex:0];
[obj setValue:@"foo" forKey:@"bar"];
Will not generate a NSKeyValueChangeSetting notification. So I manually
need to do:
[model willChange:NSKeyValueChangeSetting
valuesAtIndexes:[NSIndexSet indexSetWithIndex:0]
forKey:@"accounts"];
...
[model didChange:NSKeyValueChangeSetting
valuesAtIndexes:[NSIndexSet indexSetWithIndex:0]
forKey:@"accounts"];
It is important that these notifications be sent, because e.g. an
NSArrayController with a sort descriptor will otherwise not re-arrange
the objects, when one of the values (which affect the ordering) is
changed.
But manual implementation is not possible, because the value may be
changed from many different places, through key paths etc.
I have so far found two solutions:
o Let the model observe all keys of the collected objects and send
a notification when seeing a change - problem is that it needs to
do linear search to find the index and it will send both didChange
and willChange messages *after* the value is changed.
o Use a subclass instead of the real class for the objects being
collected - this is problematic for several reasons, mainly with
regard to "encapsulation", i.e. the one creating the object must
know which collection it will later be placed in, and choose
the proper subclass, and moving objects between collections is
not possible.
Then I read Aaron Hillegass critic of the "new" NSController system
<URL:
http://www.bignerdranch.com/Resources/nscontroller.html>, and saw
this quote: ;Mike Ferris points out that "unsavory hacks to" should
really be "clever use of".+.
And indeed this is what I want to do as well - the thing referred to is
(probably) the way that the binding system will (AFAIK) on demand (i.e.
when an observer is added to an object) replace the mutating selectors
for that object, with some which send the will/didChange messages.
But how can I do this in my code?
I know I can call class_getInstanceMethod() and simply change the
implementation function pointer - but this will change the method for
all objects of the given class. The binding system seems to only affect
individual objects (and I would prefer the same).
Furthermore, I need to attach some user data to the object being
contained (pointer to the container/model and index).
Kind regards Allan
_______________________________________________
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.