加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 是否可以将多个核心数据模型文件存储到一个xcode项目

发布时间:2020-12-15 01:49:25 所属栏目:百科 来源:网络整理
导读:参见英文答案 Is it possible to have multiple core data “databases” on one iOS app?????????????????????????????????????2个 我正在开发一个ipad应用程序,我正在处理核心数据. 应用程序管理的数据可以分为两类. 第一种数据仅适用于该设备或仅适用于ap
参见英文答案 > Is it possible to have multiple core data “databases” on one iOS app?????????????????????????????????????2个
我正在开发一个ipad应用程序,我正在处理核心数据.

应用程序管理的数据可以分为两类.

>第一种数据仅适用于该设备或仅适用于app.
>而其他类别的数据需要在具有相同应用程序的各种设备之间进行同步.

所以在scnerio中,我想到在我的项目中有两个模型文件和两个corressponding sqlite文件.并同步一个sqlite文件以实现syching.

如果我的方法是正确和可行的,请建议.如果没有,那么请建议其他解决方案.

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

谢谢.

解决方法

可能重复 here.

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

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

在为您想要的商店取出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];
    }

两家商店之间的选择.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读