Observing Changes to a mutable array using KVO vs. NSNotificationCenter

MathewS picture MathewS · May 3, 2012 · Viewed 17.1k times · Source

In my model I have an array of objects called events. I would like my controller to be notified whenever a new object is added to events.

I thought that a good way to do this would be use the KVO pattern to get notified when the events changes (from a new object being added)

// AppDelegate
// events is a NSMutableArray @property/@synthesize etc...

[appDelagate addObserver:self
               forKeyPath:@"events"
                  options:NSKeyValueObservingOptionNew
                  context:NULL];

But the observeValueForKeyPath method wasn't being called and I discovered that Arrays are not KVO compliant :-(

One option is to manually trigger the method by calling willChangeValueForKey for the keyPath

// ViewController
[self willChangeValueForKey:@"events"];
[self.events addObject:event];
[self didChangeValueForKey:@"events"];

But this feels heavy since I should probably also keep track of the before and after state of my events array so that it can be accessed from the observeValueForKeyPath method.

One approach could be to use a standard array (instead of mutable) and create/set a new instance of events each time I want to add an new object, or I could make a separate property that keeps track of how many items are in the mutable array (I wish you could observe @"events.count" ).

Another option would be to use NSNotificationCenter. I've also read some answers that suggest using blocks (but I have no idea where to start on that).

Finally, could I keep an instance of my controller in my delegate and just send a relevant message?

// Delegate
[myController eventsDidChange];

Is it odd to keep a reference to a controller from a delegate?

I'm struggling to understand how to choose which is the best approach to use, so any advice on performance, future code flexibility and best practices is greatly appreciated!

Answer

Ryder Mackay picture Ryder Mackay · May 3, 2012

You should not make direct public properties for mutable collections to avoid them mutating without your knowledge. NSArray is not key-value observable itself, but your one-to-many property @"events" is. Here's how to observe it:

First, declare a public property for an immutable collection:

@interface Model
@property (nonatomic, copy) NSArray *events;
@end

Then in your implementation back it with a mutable ivar:

@interface Model ()
{
    NSMutableArray *_events;
}
@end

and override the getter and setter:

@implementation Model

@synthesize events = _events;

- (NSArray *)events
{
    return [_events copy];
}

- (void)setEvents:(NSArray *)events
{
    if ([_events isEqualToArray:events] == NO)
    {
        _events = [events mutableCopy];
    }
}

@end

If other objects need to add events to your model, they can obtain a mutable proxy object by calling -[Model mutableArrayValueForKey:@"events"].

NSMutableArray *events = [modelInstance mutableArrayValueForKey:@"events"];
[events addObject:newEvent];

This will trigger KVO notifications by setting the property with a new collection each time. For better performance and more granular control, implement the rest of the array accessors.

See also: Observing an NSMutableArray for insertion/removal.