Skip navigation.
 
mlRe: KVO and the observeValueForKeyPath bottleneck
FROM : Jim Correia
DATE : Tue Jul 18 05:37:50 2006

On Jul 17, 2006, at 9:00 PM, Chris Kane wrote:

> The solution is that the context pointer must be used to provide a 
> globally unique value that you can recognize in your class.  If you 
> recognize the value, this notification is for you, otherwise you 
> pass the method call up to super and return.  You have to use a 
> value that the other classes in the hierarchy won't (or can't) use.
>
> You cannot use NULL as the context pointer, because the superclass 
> (or a subclass) might also use NULL.  NULL is a shared value.  You 
> can't use selectors, either, because their values are global to the 
> process, and a sub or superclass could potentially use the same 
> one.  Plus, recognizing many possible selector values would be a 
> pain and time consuming.  It's better to pick one context value.


So one potential solution here is to allocate a dictionary, use that 
as the context for your class, and also use it as a dispatch table.

Since the dictionary is dynamically allocated using it as the void * 
context will guarantee uniqueness in the same way as Chris' "ugly" 
solution in so far as all addresses within the address space are 
unique. It won't guarantee uniqueness if anyone else uses arbitrary 
context pointers like this:

static void *ObservationContext = (void *)2091;

The observer would look something like this:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)
object change:(NSDictionary *)change context:(void *)context
{
   if (context == dispatchDictionary) {
       SEL selector = NSSelectorFromString([dispatchDictionary 
objectForKey: keyPath]);
       // dispatch to self based on the selector looked up in the table
   } else {
       [super observeValueForKeyPath: keyPath ofObject: object change: 
change context: context];
   }
}

While this solves the problem Matt wanted to solve, there is still a 
fair bit of ugliness and hand waving going on.

You have to make sure you have one dispatch table per class. You have 
to keep the dispatch table up to date (as observers are registered). 
Your class has to know about the dispatch table and correctly 
implement -observeValueForKeyPath:ofObject:change:context:. It is 
left as an exercise for the reader to accomplish this correctly, 
without cut and paste programming.

If I've glossed over something (besides the intentional hand waving), 
hopefully someone will point it out.

Jim

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