Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating Core-Data Model Entity and the backed NSManagedobject subclass

Let's say I have a model entity with 3 attributes.
I then generate the NSManagedobject subclass for this entity and add custom methods along with properties to this class.
At a later day I added a couple of new attribute to this entity.

Objective
I want the backed NSManagedobject subclass to reflect these new attribute.

Problem
The only solution I see is going to {Editor->Create NSManagedobject Subclass} in Xcode, but this way overrides the class.

Question
Is there a way to update an Entity and the backed NSManagedobject subclass without overriding the class.

like image 805
Randel S Avatar asked Jul 12 '14 00:07

Randel S


2 Answers

Editing the managed object subclass by hand is fine. Let's say you added a new string attribute to the entity. You would add a property to your managed object subclass:

@property (nonatomic, copy) NSString *myAttribute;

And mark it as dynamic:

@dynamic myAttribute;

Done! Having Xcode generate the class is intended to provide a starting point. It's not doing anything super special to tie your property to an entity attribute.

like image 199
quellish Avatar answered Sep 21 '22 22:09

quellish


This is an issue that you always face with Core Data. You could obviously do what @quellish suggested and make the changes in the files manually, but a simple way to be able to add custom behavior to your NSManagedObject class and also retain the ability to auto generate your NSManagedObject .m and .h file is to use categories.

I usually add all the custom behavior in a category such as MyNSManagedObject (Management) and so whenever I update the attributes in the model, I just regenerate the parent files using the model editor, and all my custom code in the category remains intact.

like image 22
Apoorv Khatreja Avatar answered Sep 21 '22 22:09

Apoorv Khatreja