I'm building an app that is going to rely on 3 separate .sqlite databases. What methods in my App Delegate am I going to have to edit to allow for this? Right now, my managedObjectContext
and managedObjectModel
haven't been touched from how the template created them. My persistantStoreCoordinator
looks like this:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil)
{
return _persistentStoreCoordinator;
}
NSURL *storeURLConfigA = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"dbA.sqlite"];
NSURL *storeURLConfigB = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"dbB.sqlite"];
NSURL *storeURLConfigC = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"dbC.sqlite"];
// // Pre-load .sqlite db in Project Navigator into app on first run after deleting app
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURLConfigA path]])
// dbA
{
NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"dbA" ofType:@"sqlite"]];
NSError* err = nil;
if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURLConfigA error:&err])
{
NSLog(@"Error preloading database A - %@",error.description);
}
}
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURLConfigB path]])
// dbB
{
NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"dbB" ofType:@"sqlite"]];
NSError* err = nil;
if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURLConfigB error:&err])
{
NSLog(@"Error preloading database B - %@",error.description);
}
}
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURLConfigC path]])
// dbC
{
NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"dbC" ofType:@"sqlite"]];
NSError* err = nil;
if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURLConfigC error:&err])
{
NSLog(@"Error preloading database C - %@",error.description);
}
}
// // Put the Configs into the PersistantStoreCoordinator and tie them to their database file
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = @{NSSQLitePragmasOption : @{@"journal_mode": @"DELETE"}};
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Config_A" URL:storeURLConfigA options:options error:&error])
{
NSLog(@"Error setting up dbA - %@",error.description);
abort();
}
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Config_B" URL:storeURLConfigB options:options error:&error])
{
NSLog(@"Error setting up dbB - %@",error.description);
abort();
}
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Config_C" URL:storeURLConfigC options:options error:&error])
{
NSLog(@"Error setting up dbC - %@",error.description);
abort();
}
return _persistentStoreCoordinator;
}
Then, I have a single .xcdatamodelID in my mainBundle
that has all of my tables/Entities in it, for all three databases. I used XCode's CoreData editor interface to add Configurations Config_A
, Config_B
, and Config_C
, in addition to the Default
Configuration. I've moved the Entities into the Configurations for the databases I want them to be in (each Entity should only be in one database).
I run the app and everything runs fine, and I can read and write data without any problem. The problem comes when I view the tables (through the terminal or Firefox's SQLite Manager, for example). All 3 of the databases contain all of the tables, but the tables only have data in the database they are supposed to exist in (there are 0 rows in that table in the other two databases).
Why are all of the tables in each database? How can I get each database to only include the couple tables I want them to have?