Re: Design Question: Where to observe using KVO?
Re: Design Question: Where to observe using KVO?
- Subject: Re: Design Question: Where to observe using KVO?
- From: Patrick Mau <email@hidden>
- Date: Thu, 10 Jul 2008 23:55:11 +0200
I. Savant wrote:
>> Check out this page:
>>
>> http://homepage.mac.com/mmalc/CocoaExamples/controllers.html
>>
>
> I should also mention that you should approach these examples like this:
>
> 1 - Read the Cocoa Bindings documentation (including all the Key Value
> Coding / Key Value Observing related documentation).
> 2 - Examine the examples given above.
> 3 - Go back over the documentation!!! Seeing a working example makes a
> second perusal of the docs much more fruitful.
Thanks a lot for your remarks, I really appreciate it. My basic
implementation already works!
I have built a master-detail window, using an NSTable view for label
and amount. My PieChartView is observing the array like this:
> - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
> change:(NSDictionary *)change context:(void *)context
> {
> NSLog(@"observeValueForKeyPath:%@ ofObject:%p %@", keyPath, object, change);
>
> int i;
> NSArray *changedObjects;
> int changeType = [[change valueForKey:NSKeyValueChangeKindKey] intValue];
>
> switch (changeType) {
> case NSKeyValueChangeInsertion:
> changedObjects = [change valueForKey:NSKeyValueChangeNewKey];
> for (i = 0; i < [changedObjects count]; i++) {
> [self observeObject:[changedObjects objectAtIndex:i] withKeyPath:@"label"];
> [self observeObject:[changedObjects objectAtIndex:i] withKeyPath:@"amount"];
> }
> break;
This is an excerpt of the code executed whenever a new row is added to
the NSTableView. I observe the new NSDictionary provided by
NSArrayController. Deleting multiple rows also works.
I'd like to show you one ugly detail. I put all observed objects,
including their keyPath in a local NSDictionary, because I have to
remove the observers on deletion of a table row. The 'observeObject'
looks like that:
> - (void)observeObject:(id)object withKeyPath:(NSString *)keyPath {
> NSLog(@"start observing %p with keypath %@", object, keyPath);
>
> [object addObserver:self forKeyPath:keyPath
> options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
>
> [observers setValue:object forKeyPath:[NSString stringWithFormat:@"%p.%@", object, keyPath]];
> }
In my opinion, this is as ugly as it can possibly get.
I'm using the address of the observed object and append the keypath to
create a pseudo key for each observed instance.
I don't like that at all, but how would I be able to remove all
observers when I have to clean them up later?
Is there a better approach instead of observing every property of all
array entries?
Patrick
_______________________________________________
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