Best way to recreate a class definition after modifying the kernel data model?

When developing a basic data model in Xcode, you can automatically generate NSManagedObject subclass definitions (.m and .h files) on

  • Entity Selection
  • Select "Subclass NSManagedObject" on the EDITOR Menu

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

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

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

+7
source share
3 answers

You have a confused general problem. You are pretty much stuck with this way of subclassing managed objects using Xcode. Knowing this, you can:

  • Design around it

    In simple cases, you can use categories to add functionality (albeit not consisting) to subclasses of NSManagedObject . The code in the category file obviously cannot be overwritten when changing your data model.

  • Do not use Xcode

    Mogenerator is an excellent tool designed to solve this particular problem. It creates two classes for each object instead of one, allowing Xcode to control it while you control the other.

+6
source

Apple seems to have addressed a problem with Xcode 7: now it automatically creates an object and an object category with its basic data properties. When you regenerate, it updates only the category, leaving your custom code in the entity class unharmed. See Link

+1
source

You can create a class with a different name and insert the generated fields into the old class

0
source

All Articles