Search code examples
objective-ccocoacore-datansmanagedobjectnsmanagedobjectmodel

What is the relationship between NSManagedObject subclasses and the xcdatamodeld file?


  • I started creating entities in the xcdatamodeld (xcdatamodel) file.
  • I subclassed entities using the Create NSManagedObject Subclass… menu entry.
  • I added custom code to these subclasses.

If i want to make changes (like add/remove an attribute, a relationship) to an entity at this moment, where would i make these changes:

  • at the xcdatamodeld (xcdatamodel) file,
  • at my custom NSManagedObject subclass,
  • at both, the xcdatamodeld (xcdatamodel) file and my custom NSManagedObject subclass?

So, what is the relationship between NSManagedObject subclasses and the xcdatamodeld file?
Do i have to keep those two elements in sync?


Solution

  • xcdatamodeld rules :) or more specifically, the resulting managed model. Whatever is there or not will determine the store structure.

    So ...
    You make your changes in the model file first and foremost.
    This will enable you to echo these changes to the managed object subclasses.
    This is not mandatory for addition, but I believe it is mandatory for attribute/relationship removal (not tested).

    If you have custom code, I will advise you to add it in a category over the managed object subclass as this will allow you to easily regenerate your classes interface when you change your model file.