This question already has an answer here:
-
Is it possible to have multiple core data “databases” on one iOS app?
2 answers
I am working on an ipad app where i am dealing with core data.
The data managed by the app can be categorised into two categories.
- The first kind of data is specfic to that device only or app only.
- whereas the other category of data needs synching among various device having the same app.
so in the scnerio, i got a thought to have two model file in my project and two corressponding sqlite files. And syncing one sqlite file to order to achieve syching.
Please suggest, if my approach is right and feasible. If not, then please suggest other solutions.
Hey guys please try to understand the question. Here i am talking about of two sqlite files having different structure from each other. Means ".xcdatamodel" model files
Thanks.
Possible duplicate here.
You can have any number of data models, provided you create different managed object contexts for each and manage them properly.
- (NSURL *)applicationDocumentsDirectoryForCoreData
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
//first data model
NSURL *modelURL1 = [[NSBundle mainBundle] URLForResource:@"1_model" withExtension:@"momd"];
NSURL *storeURL1 = [[self applicationDocumentsDirectoryForCoreData] URLByAppendingPathComponent:@"1_model.sqlite"];
NSError *error = nil;
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL1];
persistentStoreCoordinator1 = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];
if (![persistentStoreCoordinator1 addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL1 options:nil error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
//second model.
NSURL *modelURL2 = [[NSBundle mainBundle] URLForResource:@"2_model" withExtension:@"momd"];
NSURL *storeURL2 = [[self applicationDocumentsDirectoryForCoreData] URLByAppendingPathComponent:@"2_model.sqlite"];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL2];
NSError *error = nil;
persistentStoreCoordinator2 = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
if (![persistentStoreCoordinator2 addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL2 options:nil error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
And while taking out the MOC for the store you want:
//select your store - do that in selectStore or a function like that.
NSPersistentStoreCoordinator *coordinator = [self selectStore];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
Selection between two stores.
-(NSPersistentStoreCoordinator *)selectStore
{
if(someCondtion? return persistentStoreCoordinator1: persistentStoreCoordinator2;
}
You should use a plist and store that as a file for all device specific things as I suspect it isn't updated that often. Then you can read it into a dictionary or array in one line of code.
So to answer your question, yes you can have as many coredata files as you want, but it can become a hassle to maintain