Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData - Update model class instead of creating new

I am using CoreData in my iOS application. I face a problem most of the times while creating NSManagedObject classes.

This is what I do:

  1. I create an Entity in .xcdatamodeld file.
  2. Create attributes and relationships.
  3. Choose option Editor->Create NSManagedObject Class to create .h and .m classes.
  4. In .h and .m classes, I create some of my custom methods for fetching/saving objects.

So far so good. But afterwards in future if I have to change some attributes, I repeat step 2 and 3. But this time all of my custom code written in step 4 are removed automatically.

So my question is how can I update the existing classes? Instead of using option Editor->Create NSManagedObject Class which removes all my custom code.

Any help is appreciated.

Update:

Tested both approaches (Categories and Mogernator) and looks fine to me. But I have choosen Categories of being a pure Xcode approach. I don't want to take the risk of any 3rd party which may break in future due to XCode updates or can cause problem of data migration.

Thanks to @Tom Harrington, and @Valentin Shamardin for guiding me :)

like image 707
Khawar Avatar asked Feb 07 '14 05:02

Khawar


1 Answers

To make some additional methods or other stuff for your Core Data model classes you have to create Categories. This approach is used by Paul Hegarty in Core Data lections.

like image 50
Valentin Shamardin Avatar answered Oct 05 '22 12:10

Valentin Shamardin