Getting to grips with KVO

Published
2012-06-05
Tagged

What was going to be a follow-up post got delayed, so now it’s a full post of its very own.

The problem

Here is the current setup for my app’s inspector panel.

In a separate window I have a graph, with the selected series “bolded” (line thickness increased). When I change the selection of the dropdown box in this panel:

  • Each object in the panel should update, and
  • The graph view should update

Here is how the bindings were originally setup.

This is pretty standard. But there’s a problem: I have no way of getting the graph to redraw when the selection changes.

First solution

My first solution was to set up the bindings as follows.

Then, in JRWindowController, I made methods like the following:

1
-(BOOL)visible {
2
    return [selectedSeries visible];
3
}
4
5
-(void)setVisible:(BOOL)visible {
6
    [selectedSeries setVisible:visible];
7
    [graph setNeedsDisplay:YES];
8
}

There’s a couple of problems with this code, namely:

  1. Duplication: If I end up with five different methods that all need to refresh the graph, I’m creating a bunch of code that does pretty much the same thing five times.
  2. It doesn’t work: While the graph will redraw, the controls in the panel will not. I believe this is because we’re hooking up the KVO to the WindowController‘s methods. For whatever particular reason, the controls on the panel won’t update to reflect our selection.

Second solution

It seemed this was a perfect time to learn about KVO and how to do it manually, rather than spending this whole time using Interface Builder’s GUI to do the heavy lifting. Most of it ended up being done by the following method call:

1
[selectedSeries addObserver:self
2
    forKeyPath:@"visible"
3
    options:NSKeyValueObservingOptionNew
4
    context:NULL];

Then, later in the class definition:

1
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
2
    [graphView setNeedsDisplay:YES];
3
}

Since there were several values I wanted to observe, I actually bundled the whole thing up in a for loop to iterate over them:

1
for (NSString *key in observedKeys)
2
    [selectedSeries addObserver:self
3
        forKeyPath:key
4
        options:NSKeyValueObservingOptionNew
5
        context:NULL
6
    ];

Finally, I just needed to add some code to start and stop observing series, depending on whether or not they were selected:

1
-(void)startObservingSeries {
2
    for (NSString *key in observedKeys)
3
        [selectedSeries addObserver:self
4
            forKeyPath:key
5
            options:NSKeyValueObservingOptionNew
6
            context:NULL];
7
}
8
9
-(void)stopObservingSeries {
10
    for (NSString *key in observedKeys)
11
        [selectedSeries removeObserver:self forKeyPath:key];
12
}
13
14
-(void)setSelectedSeries:(JRGraphSeries *)newSelectedSeries {
15
    if (selectedSeries != NULL)
16
        [self stopObservingSeries];
17
18
    selectedSeries = newSelectedSeries;
19
    [self startObservingSeries];
20
21
    [graphView setNeedsDisplay:YES];    
22
}

Now KVO works correctly (a change to selectedSeries triggers the right KVO methods to update panel controllers), the graph redraw works correctly (my own KVO code makes sure that happens), and even better, I’ve removed some code duplication. And, as a final cherry on the top, I understand not only how KVO works, but how to manually implement it in my own projects.