Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to recreate the class definition after modifying core data model?

When designing the core data model in XCode, you can automatically generate NSManagedObject subclass definitions (.m and .h files) by

  1. Selecting the Entities
  2. Choosing "Create NSManagedObject Subclasses" from EDITOR menu

After that, you may add a lot of code in these classes, what if you have to modify the data model setting a lot for some reason after that? To reflect these changes on the data model, is there any automatic way to do that? or you have to do everything manually.

Currently if I try to recreate these class definition from EDIT menu again(automatically), it will replace all the current files. All added code will disappear.

I really hope future version of Xcode can add a smart feature: automatically updating the default class definition without losing the added work. Maybe I am too lazy. :)

like image 632
horacex Avatar asked Aug 27 '12 17:08

horacex


2 Answers

You're running into a common problem. You're pretty much stuck with that way of creating managed object subclasses with Xcode for the time being. Knowing that, you can either:

  • Design around it

    For simple cases, you can use Categories to add functionality (though not state) to your NSManagedObject subclasses. Code in the category's file is obviously safe from being overwritten every time your data model changes.

  • Don't use Xcode

    Mogenerator is a nifty tool designed to solve exactly that problem. It creates two classes for each entity instead of one, allowing Xcode to manage one while you manage the other.

like image 148
Matt Wilding Avatar answered Oct 19 '22 23:10

Matt Wilding


It seems Apple has addressed the issue with XCode 7 : now it automatically creates the entity and a category of the entity with its core data properties. When you regenerate, it only updates the category, leaving your custom code in the entity class unharmed. See this link

like image 30
Bioche Avatar answered Oct 20 '22 01:10

Bioche