views:

559

answers:

1

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  A: 

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.

Joshua Nozzi
Thanks for the clarification. I tested this again, too. I was obviously wrong: adding transient properties to the model does not lead to incompatible stores. There also seems to be an argument for efficiency. I haven't however understood how the caching works. If I have for instance a CLLocationCoordinate2D property and the getter methods simply creates this from two doubles lat/long, how will Core-Data cache the resulting value?
Felix
Not sure, but this may be worth testing: "There are two strategies both for getting and for setting the transient value. You can retrieve the transient value either "lazily" ... or during awakeFromFetch ... It may be preferable to retrieve it lazily if the value may be large ... For the persistent value, you can either update it every time the transient value is changed (..., or you can defer the update until the object is saved ..." (trimmed to fit)
Joshua Nozzi