I have an application where I have multiple modules, each with their own ManagedObjectModel, and each model defines a configuration that includes all the entities in that model so that the data will be saved to a different NSPersistentStore (sqlite).
During application launching, I combine all these models using the following method. This is so that my application can just deal with one NSManagedObjectContext and have access to all entities in the app, without having to worry about which store the entities will save to. The app makes one NSPersistentStoreCoordinator, and then adds the different stores associated with these different models.
I followed the plugin paradigm from Marcus Zarra's Core Data and Plugins here: http://www.cimgf.com/2009/05/03/core-data-and-plug-ins/, adapting it to iOS.
The problem I am seeing is that when I look into the various sqlite files that are created by the NSPersistentStores, every store contains every entity from the union of the NSManagedObjectModels.
How can I prevent Core Data from adding the Union of all entities into every sqlite store?
Addendum: (pseudo code for what I am trying to do)
Let's say we have the following:
Two ManagedObject Models are defined, MOM1.xcdatamodeld and MOM2.xcdatamodeld, in our project.
MOM1 defines EntityA and EntityB. It defines "MOM1_Configuration" = Entities A,B.
MOM2 defines EntityC and EntityD. It defines "MOM2_Configuration" = Entities C,D.
In code, we make an array @[MOM1,MOM2] and combine them using modelByMergingModels.
MergedMOM = [NSManagedObjectModel modelByMergingModels@[MOM1, MOM2]];
We then set this mergedMOM into an NSPersistentStoreCoordinator via:
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:MergedMOM];
Finally, we add stores via:
NSURL* storeURL = [[[NSFileManager defaultManager] applicationDocumentsDirectory] URLByAppendingPathComponent:@"MOM1.sqlite"];
NSDictionary* migrationOptions = @{NSMigratePersistentStoresAutomaticallyOption : @(YES),
NSInferMappingModelAutomaticallyOption : @(YES)};
NSError* error;
NSPersistentStore* store = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:@"MOM1_Configuration"
URL:storeURL
options:migrationOptions
error:&error];
and
NSURL* storeURL = [[[NSFileManager defaultManager] applicationDocumentsDirectory] URLByAppendingPathComponent:@"MOM2.sqlite"];
NSDictionary* migrationOptions = @{NSMigratePersistentStoresAutomaticallyOption : @(YES),
NSInferMappingModelAutomaticallyOption : @(YES)};
NSError* error;
NSPersistentStore* store = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:@"MOM2_Configuration"
URL:storeURL
options:migrationOptions
error:&error];
Once this runs and entities of all types (A-D) are created / saved, Entities A,B are correctly saved to MOM1.sqlite and Entities C,D are correctly saved to MOM2.sqlite.
HOWEVER, MOM1.sqlite has empty TABLES for Entities C,D and MOM2.sqlite has empty TABLES for Entities A,B.
How do I set this up such that MOM1.sqlite does not define any TABLES for C,D and MOM2.sqlite does not define any TABLES for A,B, but in a way that I can use 1 NSPersistentStoreCoordinator, MOM, and thus one ManagedObjectContext in my app.
from Core Data - Multiple persistent stores with merged model creating union of all entities in all stores despite configuration
No comments:
Post a Comment