Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 868
Felix Lamouroux Avatar asked Nov 14 '09 13:11

Felix Lamouroux


People also ask

What is transient in Core Data?

Transient attributes are properties that you define as part of the model, but that are not saved to the persistent store as part of an entity instance's data. Core Data does track changes you make to transient properties, so they are recorded for undo operations.

What is transient property in Swift?

Transient means that the value is stored in memory on the object graph. Computed means that the value is stored nowhere and is calculated in the getter. Both are distinct from the classic non-transient attribute which is stored on the object graph and is saved to disk.

What is a transient property?

A transient property is a property that is not persisted to the database. Transient properties can have non-null values on the clipboard, but when a clipboard page containing transient properties is saved to the PegaRULES database, the values are removed from the Storage Stream column.

How do you use transformable Core Data?

To implement a Transformable attribute, configure it by setting its type to Transformable and specifying the transformer and custom class name in Data Model Inspector, then register a transformer with code before an app loads its Core Data stack.


1 Answers

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.

like image 157
Joshua Nozzi Avatar answered Sep 20 '22 14:09

Joshua Nozzi