核心数据:观察某种类型的新实体

发布于 2024-12-02 13:49:20 字数 634 浏览 2 评论 0原文

每当添加某种类型的实体(并且可能更改/删除)时,我希望收到通知。

我读到可以通过向 ManagedObjectContext 添加观察者来实现。但是,我还没有找到实际的方法来做到这一点。

我正在做:

[context addObserver:self forKeyPath:@"{myEntityName}" options:{I have tried several different values, but I am failing to understand which one to use} context:@"NewEntity"];

谢谢你的帮助。

注意:我显然对 coredata/cocoa/objective-c 很陌生,这可能是非常基本的,但一直在寻找答案太久了。找不到有关如何正确观察上下文对象的更改的示例和/或解释(我已经能够毫无问题地观察特定实体的更改)。

顺便说一句:这是一个类似的问题,表明这是可能的,但我缺乏细节: 核心数据:观察某种类型实体的所有变化

I would like to be notified whenever an entity of a certain type is added (and maybe changed/removed).

I read it is possible by adding an observer to the managedObjectContext. However, I haven't found an actual way to do it.

I am doing:

[context addObserver:self forKeyPath:@"{myEntityName}" options:{I have tried several different values, but I am failing to understand which one to use} context:@"NewEntity"];

Thanks for the help.

Note: I am obviously new to coredata/cocoa/objective-c, and this is probably very basic, but has been chasing for an answer for too long. Can't find samples and/or explanations on how to properly observe the changes for the context object (I have been able to observe changes on specific entities without issues).

BTW: this is a similar question that suggests this is possible, but I am lacking the details: Core Data: Observing all changes on Entity of certain type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-12-09 13:49:20

首先,不要混淆实体和对象。实体是类似于类的抽象,它们永远不会添加到托管对象上下文中或从托管对象上下文中删除。被管理对象是添加到被管理对象上下文中或从被管理对象上下文中删除的对象。每个托管对象都与数据模型中的实体相关,就像任何其他对象实例与特定类相关一样。

因此,您真正想要的是在插入/更新/删除特定实体的托管对象时收到通知。

处理此问题的最简单方法是注册上下文:

NSManagedObjectContextObjectsDidChangeNotification

...每当插入/更新/删除上下文中的托管对象时,它将提供通知。要仅查找特定实体的托管对象,请检查 NSInsertedObjectsKey、NSUpdatedObjectsKey 和 NSDeletedObjectsKey 键返回的对象,然后检查每个对象的 entity 属性。

或者,您可以使用自定义 NSManagedObject 子类并重写 awakeFromInsert 以便在首次插入对象时发出通知。

我要指出的是,很少需要这样的功能。当您发现自己连接了大量通知时,通常表明您的数据模型需要重新设计以捕获更多信息。您通常需要通知,因为数据模型的某些关键逻辑未编码在 Core Data 中,而是驻留在需要通知的外部对象中。

Firstly, don't confuse entities and objects. Entities are abstractions akin to classes and they are never added to or removed from a managed object context. It is the managed objects that are added to or removed from a managed object context. Each managed object is keyed to entity in the data model just like any other object instance is keyed to a particular class.

So,what you really want is to be notified when a managed object keyed to a particular entity is inserted/updated/deleted.

The easiest way to handle this is to register for the context's:

NSManagedObjectContextObjectsDidChangeNotification

… which will provide a notification whenever a managed object in the context is inserted/updated/deleted. To find only the managed objects keyed to a particular entity check the objects returned by the NSInsertedObjectsKey, NSUpdatedObjectsKey, and NSDeletedObjectsKey keys and then check the entity property of each object.

Alternatively, you use a custom NSManagedObject subclass and override awakeFromInsert to issue a notification when the object is first inserted.

I would note that such a functionality is rarely needed. When you find yourself wiring up a lot of notifications, that is usually an indication that your data model needs reworking to capture more information. You usually need notifications because some key logic of the data model is not encoded in Core Data but resides in an external object that needs the notification.

暮年慕年 2024-12-09 13:49:20

我选择这种方法,它感觉更干净:

  • 创建一个基于实体的 NSArrayController (使用 Interface Builder 编写更少的代码)
  • 观察数组控制器的 arrangedObjects 键路径
  • 完成。

I'm choosing this approach instead, it feels cleaner:

  • Create an entity-based NSArrayController (use Interface Builder to write less code)
  • Observe arrangedObjects key path of your array controller
  • Done.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文