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!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…