Observing key paths via arrrangedObjects
Observing key paths via arrrangedObjects
- Subject: Observing key paths via arrrangedObjects
- From: Daniel Jalkut <email@hidden>
- Date: Sun, 01 Jan 2006 11:20:24 -0500
I'm currently "taking in lots of information" about how to best
support undo in a bindings-based application. Apologies if this is
bordering on FAQ territory - I've reviewed many of the pertinent
threads in the archives before posting.
I'm taking the "omniscient observe" approach, as I believe mmalc
referred to it, choosing to have model changes observed and
registered with the undo manager from a centralized location.
I looked at the "RaiseMan" example from the Big Nerd Ranch solutions
code. It implements the to-many key value accessor methods to
monitor additions to and removals from a model array, and manually
starts and stops observing the properties of each object at run time.
I am not too psyched by the idea of having all this functionality in
the document class for every bindings application I ever write. At
best, I'd like to be able to accomplish the same thing with a regular
NSArrayController. At worst, I'd like a general-purpose controller
subclass that will manage these change observations for me.
I can get very close to "what I think I want" by observing an
NSArrayController's "arrangedObjects.<key>" path. For instance, if my
array controller contains objects with a key "personName":
[oPersonController addObserver:self
forKeyPath:@"arrangedObjects.personName"
options:NSKeyValueChangeSetting context:nil];
This generates a changed notification whenever *any* object in the
array gets a changed personName. Cool! The problem is, I can't tell
which object it was from my observeValueForKeyPath method! I'm still
kind of confused by key paths like the one above, where a family of
properties rather than a specific property is indicated. Is this a
standard KVO convention, or a special trick used by
NSArrayController? It seems weird that
So I guess my main questions can be summed up like this:
1. Is there any way to automatically observe an object key path
through NSController *and* know which object it was that triggered
the change?
2. If I'm going to subclass NSArrayController, are there any gotchas
or preferred techniques for doing this? I'm assuming I'll do
something similar to the Big Nerd Ranch example, manually observe all
the desired keys as objects are added to the controller's content
array. I'd like to integrate the change notifications into the
standard KVO chain, but what should the client "bind" to? I guess
I'll define some bogus conceptual key path like
"changingObjects.<key>" which a client will use to inform the array
controller that it wants to observe changes to a specific key. I
don't want to override the existing behavior of
"arrangedObjects.<key>" because that seems dangerous. When the
controller gets one of these funky addObserver: calls, it will
remember the info and basically promise to handle the "addObserver"
and "removeObserver" calls on the client's behalf. Should the
conceptual key be totally wacky to avoid possible conflict with a
future key from Apple? Maybe "changyDooDad.<key>" would be better :)
I'd love to hear your advice on this! If a subclass is in order then
I'll plan on sharing it when I'm done.
Thanks,
Daniel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden