Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: KVO and the observeValueForKeyPath bottleneck



On Jul 14, 2006, at 1:44 PM, Matt Neuburg wrote:

I'm just curious about how people are handling the fact that when you do
KVO, all your notifications are bottlenecked through a single method,
observeValueForKeyPath:... This is a very unpleasant and crude architecture
(in contrast to NSNotification where, when a notification comes in, it is
automatically routed to the selector of your choice). I really don't want a
series of "ifs" here. I can imagine a simple dispatcher architecture based
on NSSelectorFromString; is this the sort of thing people are using? Thx -
m.
--

Good question; I hope this sparks more discussion.

I can think of two approaches using the context parameter, one of which I implemented recently. In my main controller I am observing several array controllers, where the content object types vary from controller to controller. When the selection on any controller changes, I get the path to a file and display it in a viewer. When registering as an observer, I set the context to a string constant for the key path to the file name. Here is a simplified example:


-(void)awakeFromNib
{
[inputFilesArrayController addObserver:self forKeyPath:@"selection" options:nil context:@"selection.sourceFilePath"];
[processedFilesArrayController addObserver:self forKeyPath:@"selection" options:nil context:@"selection.savedToPath"];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id) object change:(NSDictionary *)change context:(void *)context
{
// In awakeFromNib:, KVO registration done with context set to the key value to retrieve.
// If it is a string (thus a path), then display it.
if (context) {
id displayPath = [object valueForKeyPath:context];


		// We may get an placeholder marker or nil
		if (displayPath && [displayPath isKindOfClass:[NSString class]]) {
			[fileViewerController open:displayPath];
		}
	}
}



This worked OK for the given situation. If I had a more complex scenario, I would consider enumerating constants to use as the context and then use a switch() in observeValueForKeyPath:. Something like this:


-(void)awakeFromNib
{
[firstArrayController addObserver:self forKeyPath:@"selection.attribute" options:nil context:EnumConstant1];
[secondArrayController addObserver:self forKeyPath:@"selection.attribute" options:nil context:EnumConstant2];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id) object change:(NSDictionary *)change context:(void *)context
{
switch (context) {
case EnumConstant1:
// ...do something...
break;
case EnumConstant2:
// ...do something else...
break;
default:
break;
}
}


I haven't tried the second example, so it may not be as suitable as I think. Comments?

Cheers,

Aaron

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >KVO and the observeValueForKeyPath bottleneck (From: Matt Neuburg <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.