KVO observing across to-many relationships
KVO observing across to-many relationships
- Subject: KVO observing across to-many relationships
- From: Roland King <email@hidden>
- Date: Fri, 26 Sep 2008 15:08:01 +0800
I managed to think myself into a corner with KVC/KVO, I was wondering
if you can automatically observe across a to-many relationship, I
can't see how you could, but cocoa often surprises me.
Simple example would be .. I have a instance, race, of a Race object
which has an array property 'competitors', it returns an NSArray* of
Competitor objects. Each Competitor has a energy property, which is an
NSInteger. As the race goes on, energy changes.
so Race has this
@property( readonly, retain ) NSArray* competitors;
and Competitor has this (as well as other properties)
@property( readonly, assign ) NSInteger *energy;
(you can assume that the insertObject:inCompetitorsAtIndex: and other
KVO methods are properly defined and used)
I can do this just fine
[ race getValueForKeyPath:@"competitors.energy" ]
and I get an NSArray of all the energies of all the competitors in
the race. getValueForKeyPath happily iterates the array property, gets
each competitor and gets the energy property of it and returns the lot.
Now I want to observe stuff, so I added
[ race addObserver:self forKeyPath:@"competitors" options:15
context:NULL ]
and all was fine and I got notified nicely as the array of competitors
changed (which it does as things start up and people give up etc).
then I added, blindly following the same pattern for the key path I'd
used in the getValueForKeyPath call, and not really thinking about it
a lot
[ race addObserver:self forKeyPath:@"competitors.energy" options:15
context NULL ]
and that failed dismally with
*** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '[<NSCFArray 0x1053c0>
addObserver:forKeyPath:options:context:] is not supported. Key path:
energy'
Thinking about it I realise I was asking rather a lot, I wanted KVO to
automatically notice changes in the array, register and unregister
observers for each of the competitor objects and tell me when their
energy level changed, I'm not even quite sure what notification I
really expected to get. Anyway, it didn't work. So I was just about to
write code which observed the competitors property, then added
notifications onto each of the objects for the keypath "energy", dealt
with additions and deletions from the competitors array etc and I
wondered .. this sounds a bit like bindings and that seems to work
across paths (ie arrangedObjects.property for table views).
Did I miss something here? Is there a way I can get KVO to do this
automagically with a @"competitors.energy" path in
addObserver:forKeyPath or is my feeling that KVO doesn't support that
correct? If it does, what on earth kind of notifications would I even
get as things were added to the competitors array (in a KVO compliant
manner of course). I thought perhaps instead of using an NSArray I
could write a class which looks like an Array, but overrides
addObserverForKeyPath .. remembers what key paths you've asked for,
and registers/unregisters them on each object added to or removed from
the array.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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