一个 Xcode 项目可以有多个核心数据模型文件吗?多个、核心、数据模型、文件

由网友(月影疏桐)分享简介:我正在开发一个 ipad 应用程序,我正在处理核心数据.I am working on an ipad app where I am dealing with core data.应用管理的数据可以分为两类.The data managed by the app can be categorised into t...

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

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.

第一类数据仅特定于该设备或应用程序.而其他类别的数据需要在具有相同应用的不同设备之间同步.

所以在场景中,我想到在我的项目中有两个模型文件和两个相应的 sqlite 文件.并同步一个sqlite文件以实现同步.

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.

请试着理解这个问题.在这里,我谈论的是两个具有不同结构的 sqlite 文件.表示.xcdatamodel"模型文件

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

推荐答案

可能重复 这里.

您可以拥有任意数量的数据模型,前提是您为每个模型创建不同的托管对象上下文并正确管理它们.

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

//第一个数据模型

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

//第二个模型.

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

在为您想要的商店取出 MOC 时:

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;
 }
阅读全文

相关推荐

最新文章