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

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

概述参见英文答案 > Is it possible to have multiple core data “databases” on one iOS app?                                    2个 我正在开发一个ipad应用程序,我正在处理核心数据. 应用程序管理的数据可以分为两类. >第一种数据仅适用于该设备或仅适用于app. >而其他类别的数据需要在具有相同 参见英文答案 > 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; }
总结

以上是内存溢出为你收集整理的iphone – 是否可以将多个核心数据模型文件存储到一个xcode项目中?全部内容,希望文章能够帮你解决iphone – 是否可以将多个核心数据模型文件存储到一个xcode项目中?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1068624.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存