是否有可能有多个核心数据模型文件到一个单一的xcode项目? [英] is it possilbe to have mutliple core data model files to one single xcode project?

查看:174
本文介绍了是否有可能有多个核心数据模型文件到一个单一的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.


  • 第一种数据仅适用于该设备或仅适用于应用程序。

  • 需要在具有相同应用程序的各种设备之间进行同步。

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

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.


嘿,请试着理解这个问题。这里我说的是两个sqlite文件具有不同的结构。意味着.xcdatamodel模型文件

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

感谢。

推荐答案

可能的重复此处

您可以有任意数量的数据模型,只要为每个数据模型创建不同的管理对象上下文并正确管理它们即可。

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

两家商店之间的选择。

-(NSPersistentStoreCoordinator *)selectStore
 {
    if(someCondtion? return persistentStoreCoordinator1: persistentStoreCoordinator2;
 }

这篇关于是否有可能有多个核心数据模型文件到一个单一的xcode项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆