views:

273

answers:

1

Is it ok to add ivars and methods to an instance of NSMangaedObject?

By "extra", I mean ivars that you don't want serialized.

Do I just add them to my NSMangaedObject subclass like any other class or do I have to take any extra precautions?

+6  A: 

You can do exactly what you described. If the ivars aren't in the entity description, they aren't part of the underlying model. Core Data actually has explicit support for NSManagedObject attributes that aren't persisted, though — they're marked "transient". If you do make custom ivars, though, you should remember to let go of the "extra" instance variables in didTurnIntoFault rather than dealloc like you would with a normal object.

Chuck
Thanks that sounds pretty easy. Any advantage in using transient attributes as opposed to just adding ivars?
Corey Floyd
Transient ivars get change tracking support from Core Data, so an object is marked changed if an transient property is modified. This lets you do custom processing in the getter/setter such as a transient property that is a non-core data type that gets serialized into an NSData attribute.
Barry Wark
It also (should—I've never really used Core Data) support undo management.
Peter Hosey