Observing Changes to a mutable array using KVO vs. NSNotificationCenter

17,221

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.

Share:
17,221
MathewS
Author by

MathewS

Product Manager at MassMutual with love for code and prototyping. Cat dad. New Zealander living in New York. Lovingly maintaining two unpopular open source projects... 🌭 Mustard, a Swift library for tokenizing strings when splitting by whitespace doesn't cut it: https://github.com/mathewsanders/Mustard 🆖 Tally & Walker, a lightweight Swift library for building probability models of n-grams. https://github.com/mathewsanders/Tally-Walker

Updated on June 05, 2022

Comments

  • MathewS
    MathewS about 2 years

    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!

  • MathewS
    MathewS about 12 years
    Thank you! mutableArrayValueForKey does the trick. Do you have any tips around how you choose which pattern to use (KVO, NotificationCenter, delegate) when you want to communicate between model to controller?
  • Alex Zavatone
    Alex Zavatone over 8 years
    I'm certainly missing something here. Could someone please explain where setEvents would get called if someone were to add or insertObject: atIndex: an object to the backing mutable array?