is it possible to have multiple core data model files to one single Xcode project? [duplicate]

谁说胖子不能爱 提交于 2019-12-18 02:54:15

问题


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 specific 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 scenario, I got a thought to have two model file in my project and two corresponding sqlite files. And synching one sqlite file to order to achieve synching.

Please suggest, if my approach is right and feasible. If not, then please suggest other solutions.

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


回答1:


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;
 }



回答2:


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



来源:https://stackoverflow.com/questions/16146289/is-it-possible-to-have-multiple-core-data-model-files-to-one-single-xcode-projec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!