Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding non-persistent variables to nsmangedobject

I have a subclass of an NSManagedObject, and I'd like to add a couple ivars to keep track of some book-keeping. I don't want these vars to persist, and so that is why I don't include them as part of the data model.

I'm having trouble finding the proper way of doing this.

  • should I just create ivars in my class, create the corresponding property and then synthesis them in the implementation?
  • should I not create ivars, and instead just declare the property and then @dynamic the property in the implmentation?
  • is there some other approach i should be taking?

And should I be doing all the customization in the my NSManagedObject subclass, or should I be creating a subclass of my subclass, so that if I change my data model I won't lose all my customizations when I get xcode to generate my NSManagedObject subclass automatically?

Thanks!

like image 553
Wise Shepherd Avatar asked Feb 11 '12 19:02

Wise Shepherd


3 Answers

The documentation for NSManagedObject has a section called Custom Instance Variables. It discusses both approaches.

Both transient, modeled attributes and ivars are good solutions. It depends on taste and style.

One very important point to remember if you use ivars: You need to clear out ivars in - (void)didTurnIntoFault and set any initial values in - (void)awakeFromFetch and - (void)awakeFromInsert. You need to not touch any modeled attributes or relationships inside -didTurnIntoFault or else you'll get in trouble.

like image 62
Daniel Eggert Avatar answered Sep 27 '22 18:09

Daniel Eggert


Each attribute for NSManagedObject has a checkbox named transient. This way you'll have dynamic accessors for the object without actually persisting the property value itself. Look for checkbox right under the text field for attribute name entry.

enter image description here

UPDATE If you don't want to create a migration because of new attributes, make standard ivars

@interface MyMO : NSManagedObject {
    NSString *_nonPersistentAttribute;
}

@property (nonatomic, retain) NSString *nonPersistentAttribute;

@end

@implementation MyMO

@synthesize nonPersistentAttribute=_nonPersistentAttribute;

@end
like image 23
Eimantas Avatar answered Sep 27 '22 18:09

Eimantas


Wise,

To your first question re: ivars, you have two choices standard ivars or transient attributes on your entity. The big difference between the two is that the transient attributes participate in the change/dirty/undo aspects of Core Data. If your ivars don't need that, then don't use the transient attributes. (Yes, use @property and @synthesize for your ivars.)

To your second question re: where to make the changes? I am somewhat of a luddite. The header patterns automatically generated by Xcode are pretty simple. Hence, I use the auto generated files for the first time I create an entity and edit in my changes thereafter. Frankly, you don't change your model data structures very often. Hence, adding a few lines here and there to both .h&.m files isn't a big cost. There are other mechanisms. Wolf Rentzch's mogenerator system is well respected and useful. That said, I'm not convinced that mogenerator solves a problem faced by modern Objective-C v2 Core Data programmers. (Obj-C v2 has made many things easier for Core Data programmers.)

Andrew

like image 39
adonoho Avatar answered Sep 27 '22 16:09

adonoho