使用KVO与NSNotificationCenter观察对可变数组的更改 [英] Observing Changes to a mutable array using KVO vs. NSNotificationCenter

查看:150
本文介绍了使用KVO与NSNotificationCenter观察对可变数组的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型中,我有一个名为事件的对象数组。我希望我的控制器可以在新对象被添加到事件时被通知。

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.

我认为这样做的好方法是使用KVO模式来获取通知当事件发生变化(来自新增的对象)

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];

但是没有调用 observeValueForKeyPath 方法,我发现数组不符合KVO: - (

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

一个选项是通过调用keyPath

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"];

但这感觉很重,因为我也应该保持跟踪我的事件数组的前后状态,以便可以从 observeValueForKeyPath 方法访问。

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.

一种方法可能是使用一个标准数组(而不是可变的),并且每当我想要添加一个新对象时创建/设置一个新的事件实例,或者我可以创建一个单独的属性来跟踪可变数组中有多少项(希望你可以观察@events.count)。

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" ).

另一个选项将是使用NSNotificationCenter。我也读了一些建议使用块的答案(但我不知道从哪里开始)。

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!

推荐答案

您不应该对可变集合进行直接的公共属性,以避免在不知情的情况下变异。 NSArray 不是键值观察本身,而是您的一对多属性 @events是。以下是如何观察:

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

然后在实现中使用可变的ivar:

Then in your implementation back it with a mutable ivar:

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

并覆盖getter和setter:

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

如果其他对象需要向模型添加事件,他们可以通过调用 - [Model mutableArrayValueForKey:@events] 获取可变代理对象。

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];

这将通过每次设置新集合的属性来触发KVO通知。为了更好的性能和更精细的控制,请执行数组访问器

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.

另请参见:浏览NSMutableArray进行插入/删除

这篇关于使用KVO与NSNotificationCenter观察对可变数组的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆