Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sascha's answer got me on the right track. Merging a compiled <code>.mom</code> file from a static library into the <code>.mom</code> file from a host project was relatively simple. Here's a trivial example:</p> <ol> <li><p>Create a new XCode Static Library project called <code>MyStaticLibrary</code></p></li> <li><p>Create an .xcdatamodel file in <code>MyStaticLibrary</code> called <code>MyStaticLibraryModels.xcdatamodel</code>, add some <code>Entity</code>s, then generate the headers and implementations. When you build the <code>MyStaticLibrary</code> target, you'll generate a <code>libMyStaticLibrary.a</code> binary file, but it won't include the compiled <code>.mom</code> file. For that we have to create a bundle.</p></li> <li><p>Create a new build target of type <code>Loadable Bundle</code>, found under <code>MacOS X &gt; Cocoa</code>, let's call the new Target <code>MyStaticLibraryModels</code>. </p></li> <li><p>Drag <code>MyStaticLibraryModels.xcdatamodel</code> into the <code>Compile Sources</code> build phase of the <code>MyStaticLibraryModels</code> Target. When you build the <code>MyStaticLibraryModels</code> Target, you will generate a file called <code>MyStaticLibraryModels.bundle</code> and it will contain the compiled <code>NSManagedObjectModel</code> file, <code>MyStaticLibraryModels.mom</code>.</p></li> <li><p>After building both the <code>MyStaticLibrary</code> and <code>MyStaticLibraryModels</code> Targets, drag <code>libMyStaticLibrary.a</code> (along with any associated Model header files) and <code>MyStaticLibraryModels.bundle</code> into your host project, <code>MyAwesomeApp</code>.</p></li> <li><p><code>MyAwesomeApp</code> uses <code>CoreData</code>, has it's own <code>.xcdatamodel</code> file which will get compiled into a .mom file during its own build process. We want to merge this <code>.mom</code> file with the one we imported in <code>MyStaticLibraryModels.bundle</code>. Somewhere in the <code>MyAwesomeApp</code> project, there is a method that returns <code>MyAwesomeApp</code>s <code>NSManagedObjectModel</code>. The Apple generated template for this method looks like this:</p></li> </ol> <p>...</p> <pre><code>- (NSManagedObjectModel *)managedObjectModel { if (managedObjectModel_ != nil) { return managedObjectModel_; } NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyAwesomeApp" withExtension:@"momd"]; managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; return managedObjectModel_; } </code></pre> <p>We will alter this to merge and return BOTH of our <code>NSManagedObjectModel</code>s, <code>MyAwesomApp</code>s and <code>MyStaticLibraryModels</code>, as a single, combined <code>NSManagedObjectModel</code> like so:</p> <pre><code>- (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_; } </code></pre> <p>This will return the merged <code>NSManagedObjectModel</code> with the <code>Entity</code>s from both <code>MyAwesomeApp</code> and <code>MyStaticLibrary</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload