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

is it possible to have multiple core data model files to one single Xcode project?(一个 Xcode 项目可以有多个核心数据模型文件吗?)

本文介绍了一个 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 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;
 }

这篇关于一个 Xcode 项目可以有多个核心数据模型文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:一个 Xcode 项目可以有多个核心数据模型文件吗?

基础教程推荐