Question

Being fairly new to CoreData (Started playing with it 2 hours ago ;) ) I keep wondering if there is an easier way to do things.

I'm currently using a DataModel to create my persistant objects code.

However when I keep adjusting things like number of attributes sotred, the name of the fields etc... and that leads to having the update the code of my NSManagedObjects.

I found that you can create the classes with the attributes already implemented, however, I haven't figured out how to update the code of an existing class other than adding it by hand or creating a new object and overwriting the existing one? (or how do other developers do it)

I'm aware that the answer is surely: "there is no better way" but I still have to ask.

Was it helpful?

Solution

Check out mogenerator.

This command line utility will generate two class files. A machine generated _MyClass header and implementation, with all code for your model. It will also create a subclass MyClass where you can add your own customizations.

Each time you regenerate your code, only the machine generated _MyClass files will be updated. All your changes to MyClass will be preserved.

OTHER TIPS

If you can not use mogenerator, you can use the native code generation. Make sure you regenerate the code every time. However, that will erase any changes.

So, you have two very viable options.

  1. Write a category on the entity class. You can do just about everything you want with a category.

  2. Subclass from the entity, and put all your code in there.

Personally, I prefer using categories unless I absolutely must inherit... which is extremely rare.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top