是否有可能将mutliple核心数据模型文件包含到一个xcode项目中?(复制)

时间:2023-01-22 17:51:22

This question already has an answer here:

这个问题已经有了答案:

I am working on an ipad app where i am dealing with core data.

我正在开发一个ipad应用程序,我正在处理核心数据。

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.

所以在scnerio中,我想到在我的项目中有两个模型文件和两个corresding sqlite文件。并同步一个sqlite文件以实现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

各位,请试着理解这个问题。这里我讨论的是两个sqlite文件,它们的结构不同。意思是“。xcdatamodel”模型文件

Thanks.

谢谢。

2 个解决方案

#1


17  

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:

在为你想要的商店办理MOC时:

//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


-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.

您应该使用plist并将其存储为针对所有设备特定内容的文件,因为我怀疑它没有经常更新。然后你可以在一行代码中把它读入字典或数组。

So to answer your question, yes you can have as many coredata files as you want, but it can become a hassle to maintain

为了回答你的问题,是的,你可以有你想要的尽可能多的coredata文件,但是维护它会变得很麻烦

#1


17  

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:

在为你想要的商店办理MOC时:

//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


-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.

您应该使用plist并将其存储为针对所有设备特定内容的文件,因为我怀疑它没有经常更新。然后你可以在一行代码中把它读入字典或数组。

So to answer your question, yes you can have as many coredata files as you want, but it can become a hassle to maintain

为了回答你的问题,是的,你可以有你想要的尽可能多的coredata文件,但是维护它会变得很麻烦