I created my own class that I want Core Data to use instead of NSManagedObject
:
@interface MyManagedObject: NSManagedObject {
id delegate;
}
I can't use a category since this declares an ivar. All of my Core Data objects use concrete classes rather than being instances of NSManagedObject
. I use the code generator to generate these files. Here's an example of what a Contact
object might look like:
@interface Contact: NSManagedObject {
}
I know that I can manually change NSManagedObject
to MyManagedObject
in this generated code, but I will need to do this every time I regenerate the code. Is there any way to make it automatically use my own class?
Additionally, the #import
in any solution might be a problem. Ideally I want it to use @class MyManagedObject
rather than #import "MyManagedObject.h"
, since MyManagedObject.h
is located in a library and the header needs a folder prefix to be accessible (e.g. #import "MyLib/MyManagedObject.h"
).
I tried creating a dummy object in the .xcdatamodel
file with the same name and specifying that all objects inherit from it, but there are two problems. The first is that it is using #import "MyManagedObject.h"
, which it can't find due to the reason I specified above. The second problem is that I'm not sure it's a good idea to fool Core Data into thinking the class is inheriting from another Core Data object... even if I'm not generating the code file. I'm guessing that Core Data might be doing some unnecessary things behind the scenes because it believes there is an extra class which my classes are inheriting from.
I guess I can solve the first problem using another layer of classes. For example, I would specify that the objects inherit from MyProjectManagedObject
and create a MyProjectManagedObject.h
file in my project:
#import "MyLib/MyManagedObject.h"
@interface MyProjectManagedObject: MyManagedObject {
}
... and now my auto generated files will look like this:
#import "MyProjectManagedObject.h"
@interface Contact: MyProjectManagedObject {
}
... which will work. The only problem is the second one I mentioned above about extra code running behind the scenes in Core Data. Is this something to worry about or should I ignore it? Are there any other ways to do this which solve both problems mentioned above?