CoreData 中的瞬态属性何时应包含在对象模型中?

发布于 2024-08-11 00:36:01 字数 254 浏览 7 评论 0原文

我不确定瞬态属性的正确定义:

  1. 可以在对象模型中定义瞬态属性,然后在相关类需要时计算它们。
  2. 但是,如果您指定一个类并为任何瞬态属性定义任意 getter 方法,而无需在对象模型中声明它(只要该实体链接到模型中的该类),那么事情也同样有效。

我的问题:在对象模型中声明瞬态属性有什么好处? 我只看到一个缺点:每次添加瞬态属性(例如“FormattedDate”)时,持久存储都会变得不兼容。

我缺少什么?

I am unsure about the correct definition of transient properties:

  1. One can define transient properties in the object model and then calculate them when needed in the related class.
  2. But things work just as well if you specify a class and define arbitrary getter methods for any transient property without declaring it in the object model (as long as the entity is linked to that class in the model).

My question: What is the benefit of declaring transient properties in the object model?
I only see one down-side: each time you add a transient property (e.g. "FormattedDate") the persistentStore becomes incompatible.

what am I missing?

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

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

发布评论

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

评论(1

与往事干杯 2024-08-18 00:36:01

来自核心数据编程指南

如果不支持的属性是对象,则在托管对象模型中将其类型指定为未定义,并且它是瞬态的。当您实现实体的自定义类时,无需为属性添加实例变量 - 您可以使用托管对象的私有内部存储。关于下面描述的实现需要注意的一点是它们缓存瞬态值。这使得获取价值更加高效——这对于变革管理也是必要的。如果您定义了自定义实例变量,则应该在 didTurnIntoFault 中清理这些变量,而不是 dealloc 或 Finalize。

我认为这意味着“方便”和“将所有属性定义在一个地方 - 托管对象模型”。

至于 MOM 版本控制,核心数据模型版本控制和数据迁移编程指南 说:

Core Data 对版本控制的看法是,它只对影响持久性的模型功能感兴趣。

然而,它没有阐明其在瞬态属性上的立场。事实上,阐述该段落的第二个要点听起来几乎是一个矛盾。一个快速测试(带有一个带有“name”属性的简单“Foo”实体的新项目,保存一个包含多个foo的文件,添加一个瞬态属性,再次运行,并且foo全部加载,添加一个新的foo,保存,关闭,重新打开)显示版本控制系统实际上不考虑瞬态属性。

From the Core Data Programming Guide:

If the non-supported attribute is an object, then in the managed object model you specify its type as undefined, and that it is transient. When you implement the entity’s custom class, there is no need to add an instance variable for the attribute—you can use the managed object's private internal store. A point to note about the implementations described below is that they cache the transient value. This makes accessing the value more efficient—it is also necessary for change management. If you define custom instance variables, you should clean up these variables in didTurnIntoFault rather than dealloc or finalize.

I take this to mean "convenience" and "keeping all your attributes defined in one place - the Managed Object Model".

As for MOM versioning, the Core Data Model Versioning and Data Migration Programming Guide says:

Core Data’s perspective on versioning is that it is only interested in features of the model that affect persistence.

It doesn't clarify its position on transient properties, however. In fact the second bullet point elaborating that paragraph almost sounds like a contradiction. A quick test (new project with a simple "Foo" entity with a "name" attribute, save a file with several foos, add a transient property, run again, and the foos all load, add a new foo, save, close, re-open) shows transient properties in fact are not considered by the versioning system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文