Skip navigation.
 
mlRe: KVO and the observeValueForKeyPath bottleneck
FROM : Aaron Burghardt
DATE : Sat Jul 15 22:56:48 2006

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

Related mailsAuthorDate
mlKVO and the observeValueForKeyPath bottleneck Matt Neuburg Jul 14, 19:44
mlRe: KVO and the observeValueForKeyPath bottleneck George Orthwein Jul 15, 07:36
mlRe: KVO and the observeValueForKeyPath bottleneck Aaron Burghardt Jul 15, 22:56
mlRe: KVO and the observeValueForKeyPath bottleneck Jakob Olesen Jul 17, 12:38
mlRe: KVO and the observeValueForKeyPath bottleneck Jakob Olesen Jul 17, 13:03
mlRe: KVO and the observeValueForKeyPath bottleneck Matt Neuburg Jul 17, 16:38
mlRe: KVO and the observeValueForKeyPath bottleneck Jakob Olesen Jul 17, 16:57
mlRe: KVO and the observeValueForKeyPath bottleneck Matt Neuburg Jul 17, 17:31
mlRe: KVO and the observeValueForKeyPath bottleneck Chris Kane Jul 17, 18:09
mlRe: KVO and the observeValueForKeyPath bottleneck Matt Neuburg Jul 17, 19:49
mlRe: Re: KVO and the observeValueForKeyPath bottleneck Michael Ash Jul 17, 20:11
mlRe: KVO and the observeValueForKeyPath bottleneck Chris Kane Jul 17, 20:24
mlRe: KVO and the observeValueForKeyPath bottleneck Matt Neuburg Jul 17, 20:31
mlRe: KVO and the observeValueForKeyPath bottleneck Scott Anguish Jul 17, 22:00
mlRe: KVO and the observeValueForKeyPath bottleneck Chris Kane Jul 17, 22:42
mlRe: KVO and the observeValueForKeyPath bottleneck Jakob Olesen Jul 17, 23:16
mlRe: KVO and the observeValueForKeyPath bottleneck Matt Neuburg Jul 18, 00:43
mlRe: KVO and the observeValueForKeyPath bottleneck Chris Kane Jul 18, 03:00
mlRe: KVO and the observeValueForKeyPath bottleneck Jim Correia Jul 18, 05:37
mlRe: KVO and the observeValueForKeyPath bottleneck Jakob Olesen Jul 18, 16:06
mlRe: KVO and the observeValueForKeyPath bottleneck Jim Correia Jul 18, 18:11
mlRe: KVO and the observeValueForKeyPath bottleneck Matt Neuburg Jul 19, 02:44
mlRe: KVO and the observeValueForKeyPath bottleneck George Orthwein Jul 20, 21:25
mlRe: KVO and the observeValueForKeyPath bottleneck Scott Anguish Jul 20, 22:14
mlRe: KVO and the observeValueForKeyPath bottleneck Matt Neuburg Jul 21, 03:57
mlRe: KVO and the observeValueForKeyPath bottleneck Scott Anguish Jul 21, 10:11
mlRe: KVO and the observeValueForKeyPath bottleneck George Orthwein Jul 21, 17:46
mlRe: KVO and the observeValueForKeyPath bottleneck Scott Anguish Jul 21, 23:01