是否可以将多个核心数据模型文件用于一个 Xcode 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16146289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is it possible to have multiple core data model files to one single Xcode project?
提问by Harshit Gupta
I am working on an ipad app where I am dealing with core data.
我正在开发一个 ipad 应用程序,我正在处理核心数据。
The data managed by the app can be categorised into two categories.
应用程序管理的数据可以分为两类。
- The first kind of data is specific to that device only or app only.
- whereas the other category of data needs synching among various device having the same app.
- 第一种数据仅特定于该设备或应用程序。
- 而另一类数据需要在具有相同应用程序的各种设备之间进行同步。
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.
所以在这个场景中,我想到在我的项目中有两个模型文件和两个相应的 sqlite 文件。并同步一个sqlite文件以实现同步。
Please suggest, if my approach is right and feasible. If not, then please suggest other solutions.
请建议,如果我的方法是正确和可行的。如果没有,请提出其他解决方案。
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
请试着理解这个问题。这里我说的是两个结构不同的 sqlite 文件。表示“.xcdatamodel”模型文件
回答by Satheeshwaran
Possible duplicate here.
此处可能重复。
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];
}
//first data model
//第一个数据模型
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();
}
//second model.
//第二个模型。
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();
}
And while taking out the MOC for the store you want:
在为您想要的商店取出 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];
}
Selection between two stores.
在两家商店之间进行选择。
-(NSPersistentStoreCoordinator *)selectStore
{
if(someCondtion? return persistentStoreCoordinator1: persistentStoreCoordinator2;
}
回答by Trausti Thor
You should use a plist and store that as a file for all device specific things as I suspect it isn't updated that often. Then you can read it into a dictionary or array in one line of code.
您应该使用 plist 并将其存储为所有设备特定内容的文件,因为我怀疑它不会经常更新。然后,您可以在一行代码中将其读入字典或数组。
So to answer your question, yes you can have as many coredata files as you want, but it can become a hassle to maintain
因此,要回答您的问题,是的,您可以拥有任意数量的 coredata 文件,但是维护起来会很麻烦