Updating Core-Data Model Entity and the backed NSManagedobject subclass
Asked Answered
D

3

5

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.

Dermatologist answered 12/7, 2014 at 0:27 Comment(0)
P
8

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.

Pegmatite answered 12/7, 2014 at 0:34 Comment(1)
Use a category and you will be able to re-gen subclasses whenever you make a change in the editor without affecting the overriding behavior in your category.Bouley
S
3

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.

Shylashylock answered 12/7, 2014 at 18:0 Comment(1)
We do a similar thing. We write a .extension file that extends the subclass and then add any functions or additional code in there so overriding won't be a problem.Stonework
F
0

You can do this:

Choose "Create NSManagedObject Subclass…" from the Core Data editor menu to delete and recreate this implementation file for your updated model.

You will then remove the files you already had, and the new ones will be created. It's actually our beloved Xcode IDE that suggests we do this, as can be seen from the attached screenshot.Screenshot

Fluter answered 14/11, 2015 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.