views:

23

answers:

2

The Core Data documentation says that objects may fault to save memory if needed. What happens if you have an object that has a property that is not a managed property?

For example, say you have a Department class, that is a subclass of NSManagedObject. It has a location iVar + accessors. The location property is not an attribute of Department; it is not managed and never persisted.

If you have an array of Department objects, or an Employee object with a to-one relationship to Department, is it possible for the Department to ever fault? If you set the Department.location, can you be sure that the location will always be there, or is it possible that the Department will fault, and then you will have lost the value stored in location?

A: 

I would not trust unmanaged data to remain there. Even if it does now, the behavior may change in the future. More so, architecturally I would not recommend keeping unmanaged data in the managed object at all. You're better off:

  1. Making it managed data, OR
  2. Making the unmanaged data computable from the managed data, OR
  3. Making an object that has the unmanaged data and the Department as ivars.
John Franklin
Using 2. above, I used transient properties to store location values; seems to work okay. Anybody used different approach?
petert
A: 

It is still possible for the Department to fault, it will just fault properties you have described in your model. Typically the situation you are describing is covered by "transient" properties, which are properties that are not stored in CoreData, however the object model is aware of them.

When you implement a transient property you provide the storage (or computation) required for providing the value for that property.

In your case it is perfectly legitimate to assume that your "location" value will not be there in the future, as it will only survive so long as the actual managed object remains in memory. In other words, any action which causes the managed object to be released such as a context reset, save or update from a save notification may cause the value to be lost (as the managed object it is tied to is turned into a fault or invalidated).

ImHuntingWabbits