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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With