How can I make all Core Data objects inherit from my class rather than NSManagedObject?

后端 未结 2 984
暗喜
暗喜 2021-01-15 17:02

I created my own class that I want Core Data to use instead of NSManagedObject:

@interface MyManagedObject: NSManagedObject {
  id delegate;
}
<         


        
相关标签:
2条回答
  • 2021-01-15 17:38

    Take a look at MOGenerator. It'll help with the regenerating the managed object class files at least: it makes you two files for each one. one that you edit and one that is automatically generated. When you regenerate the latter, the former is untouched.

    http://digitalflapjack.com/blog/2010/mar/26/mogeneratorftw/

    0 讨论(0)
  • 2021-01-15 17:53

    Don't use inheritance in your data model if you're using the SQL backend. Because of the implementation of the SQL backend, it has horrible performance and space characteristics. (This is Apple's recommendation.)

    I may be wrong (I'll double-check), but I think you can do what you want using just the class and header files, without messing with the data model. (This is assuming you don't want to actually store your ivar in the data backend.) Just implement MyManagedObject like you did, and make your subclasses inherent from MyManagedObject instead of NSManagedObject (e.g. Contact : MyManagedObject). Note that you only have to do this in the header files, and not the actual data model. The compiler should figure out the rest.

    0 讨论(0)
提交回复
热议问题