Sascha’s answer got me on the right track. Merging a compiled .mom file from a static library into the .mom file from a host project was relatively simple. Here’s a trivial example:
-
Create a new XCode Static Library
project calledMyStaticLibrary -
Create an .xcdatamodel file in
MyStaticLibrarycalledMyStaticLibraryModels.xcdatamodel, add someEntitys, then generate the headers and implementations. When you build theMyStaticLibrarytarget, you’ll generate alibMyStaticLibrary.abinary file, but it won’t include the compiled.momfile. For that we have to create a bundle. -
Create a new build target of type
Loadable Bundle, found underMacOS X > Cocoa, let’s call the new TargetMyStaticLibraryModels. -
Drag
MyStaticLibraryModels.xcdatamodelinto theCompile Sourcesbuild phase of theMyStaticLibraryModelsTarget. When you build theMyStaticLibraryModelsTarget, you will generate a file calledMyStaticLibraryModels.bundleand it will contain the compiledNSManagedObjectModelfile,MyStaticLibraryModels.mom. -
After building both the
MyStaticLibraryandMyStaticLibraryModelsTargets, draglibMyStaticLibrary.a(along with any associated Model header files) andMyStaticLibraryModels.bundleinto your host project,MyAwesomeApp. -
MyAwesomeAppusesCoreData, has it’s own.xcdatamodelfile which will get compiled into a .mom file during its own build process. We want to merge this.momfile with the one we imported inMyStaticLibraryModels.bundle. Somewhere in theMyAwesomeAppproject, there is a method that returnsMyAwesomeAppsNSManagedObjectModel. The Apple generated template for this method looks like this:
…
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyAwesomeApp" withExtension:@"momd"];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
}
We will alter this to merge and return BOTH of our NSManagedObjectModels, MyAwesomApps and MyStaticLibraryModels, as a single, combined NSManagedObjectModel like so:
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSMutableArray *allManagedObjectModels = [[NSMutableArray alloc] init];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyAwesomeApp" withExtension:@"momd"];
NSManagedObjectModel *projectManagedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
[allManagedObjectModels addObject:projectManagedObjectModel];
[projectManagedObjectModel release];
NSString *staticLibraryBundlePath = [[NSBundle mainBundle] pathForResource:@"MyStaticLibraryModels" ofType:@"bundle"];
NSURL *staticLibraryMOMURL = [[NSBundle bundleWithPath:staticLibraryBundlePath] URLForResource:@"MyStaticLibraryModels" withExtension:@"mom"];
NSManagedObjectModel *staticLibraryMOM = [[NSManagedObjectModel alloc] initWithContentsOfURL:staticLibraryMOMURL];
[allManagedObjectModels addObject:staticLibraryMOM];
[staticLibraryMOM release];
managedObjectModel_ = [NSManagedObjectModel modelByMergingModels:allManagedObjectModels];
[allManagedObjectModels release];
return managedObjectModel_;
}
This will return the merged NSManagedObjectModel with the Entitys from both MyAwesomeApp and MyStaticLibrary.