When should transient properties in CoreData be included in the object model?

我的梦境 提交于 2019-12-08 16:23:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/1734189/when-should-transient-properties-in-coredata-be-included-in-the-object-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!