Observing Changes to a mutable array using KVO vs. NSNotificationCenter

前端 未结 2 1744
灰色年华
灰色年华 2020-12-13 20:35

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 t

相关标签:
2条回答
  • 2020-12-13 20:47

    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.

    0 讨论(0)
  • 2020-12-13 20:57

    Per the docs on accessor methods, you should implement:

    - (void)addEventsObject:(Event*)e
    {
        [_events addObject:e];
    }
    
    - (void)removeEventsObject:(Event*)e
    {
        [_events removeObject:e];
    }
    

    Then KVO will fire the notifications when these are called.

    0 讨论(0)
提交回复
热议问题