什么时候CoreData中的瞬态属性应该包含在对象模型中?

时间:2022-08-13 07:31:34

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. 可以在对象模型中定义瞬态属性,然后在相关类中需要时计算它们。

  3. 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).
  4. 但是,如果您指定一个类并为任何瞬态属性定义任意getter方法而不在对象模型中声明它(只要该实体链接到模型中的该类),那么事情也会起作用。

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.

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

what am I missing?

我错过了什么?

1 个解决方案

#1


6  

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.

如果不支持的属性是对象,则在托管对象模型中将其类型指定为未定义,并且它是瞬态的。实现实体的自定义类时,无需为属性添加实例变量 - 您可以使用托管对象的私有内部存储。关于下面描述的实现的注意事项是它们缓存瞬态值。这使得访问价值更有效 - 它也是变更管理的必要条件。如果定义自定义实例变量,则应在didTurnIntoFault中清除这些变量,而不是dealloc或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:

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

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

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

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.

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

#1


6  

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.

如果不支持的属性是对象,则在托管对象模型中将其类型指定为未定义,并且它是瞬态的。实现实体的自定义类时,无需为属性添加实例变量 - 您可以使用托管对象的私有内部存储。关于下面描述的实现的注意事项是它们缓存瞬态值。这使得访问价值更有效 - 它也是变更管理的必要条件。如果定义自定义实例变量,则应在didTurnIntoFault中清除这些变量,而不是dealloc或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:

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

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

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

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.

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