When designing the core data model in XCode, you can automatically generate NSManagedObject subclass definitions (.m and .h files) by
- Selecting the Entities
- 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. 🙂
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
NSManagedObjectsubclasses. 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.