使用 KVO 观察 Objective-C 中集合内对象属性的变化 [英] Using KVO to observe changes to a property on an object inside a collection in Objective-C

查看:23
本文介绍了使用 KVO 观察 Objective-C 中集合内对象属性的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Core Data 对象 (AGroup),它与一组其他 Core Data 对象(项目,它们是 ANItem 的实例)的集合具有一对多关系.我在 UITableView 中使用 AGroup 对象(这是在 iOS 上),项目的显示顺序由项目上的属性 (sortProperty) 决定.所以,代码明智(忽略它们是核心数据对象,所以这是半伪代码)它看起来像这样:

I have a Core Data object (AGroup) with a to-many relationship to a collection of other Core Data objects (items, which are instances of ANItem). I'm using the AGroup object in a UITableView (this is on iOS) and the order in which the items are displayed is determined by a property on the items (sortProperty). So, code wise (ignoring that they are Core Data objects, so this is semi-pseudocode) it looks like this:

@interface AGroup : NSManagedObject
{
    NSSet *items; // these are instances of ANItem
}
@property (retain) NSSet *items;
@end

@interface ANItem : NSManagedObject
{
    NSNumber *sortProperty;
}
@property (retain) NSNumber *sortProperty;
@end

好的,这里的重点是 UITableView 有一个 AGroup 对象的实例并且正在显示项目列表,并且项目的显示顺序由 sortProperty 属性决定.

Ok, so, the point here is that the UITableView has an instance of an AGroup object and is displaying the list of items, and the order in which the items are displayed is determined by the sortProperty property.

所以,我想知道 ANItem 实例上的 sortProperty 何时更改,以便我可以更新表视图.我想知道,有没有一种方法可以使用 KVO 从 AGroup 对象观察 sortProperty?类似的东西:

So, I want to know when the sortProperty changes on an instance of ANItem so that I can update the table view. I'm wondering, is there a way I can use KVO to observe the sortProperty from the AGroup object? Something like:

[group addObserver:self forKeyPath:@"items.sortProperty" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];

甚至:

[group addObserver:self forKeyPath:@"items.@sum.sortProperty" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];

会起作用,因为当值改变时总和会改变.

would work, since the sum would change when the value changes.

不过,我已经试过了,但我收到了一个无效的参数错误.

I've tried this, though, and I get an invalid argument error.

有谁知道这是否可能?如果是,怎么办?

Does anyone know if this is possible? If so, how?

我意识到我可以执行获取请求并通过 sortProperty 对请求进行排序,但是由于其他一些应用程序架构限制,在这种情况下这实际上是不可能的.所以本质上,这归结为想象我只是在处理对象关系中的一个普通的旧 NSSet.我只提到核心数据方面,以防因其他限制/等而影响答案?

I realize I could do a fetch request and sort the request by sortProperty, but due to some other application architecture limitations, that's not really possible in this case. So essentially, this kinda comes down to imagining that I'm just dealing with a plain old NSSet in an object kind of relationship. I only mention the core data aspect in case that impacts the answer due to additional limitations/etc?

推荐答案

我很确定没有办法像这样直接观察集合的内容.我认为最好的办法是定义符合 KVC/KVO 的集合访问器/修改器,观察 self.items,并使用更改通知在每个对象添加到集合时直接观察它们的 sortProperty.

Im pretty sure there's no way to directly observe the contents of a collection like that. I think the best bet is to define the KVC/KVO compliant set accessors/mutators, observe self.items, and use the change notifications to directly observe sortProperty on each of the objects as they are added to the collection.

由于这是核心数据,您可能还/需要使用上下文保存通知来检测项目的更改.不过,我对 Core Data 没有足够的经验可以肯定地说.

Since this is Core Data, you may also/instead need to use the context save notifications to detect changes in items. I don't have enough experience with Core Data to say for sure though.

这篇关于使用 KVO 观察 Objective-C 中集合内对象属性的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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