多模型的iPhone核心数据迁移 [英] iPhone Core Data Migration With Multiple Models
问题描述
我在我的项目有两个模型,我想允许版本到其中一个。这是我做的:
I have two models at my project, I want to allow versions to one of them. Here's what I did:
- 选择modelOne.xcdatamodel,然后选择Design> Data Model> Add Model Version。
- 点击的命令+ i然后将版本添加到modelOne.xcdatamodeld内的modelOne.xcdatamodel
- 修改modelOne.xcdatamodel,只是添加了一些属性。
-
在委托我添加了选项字典到NSPersistentStoreCoordinator,我得到NSManagedObjectModel通过合并两个模型像:
- Selected the modelOne.xcdatamodel then Design > Data Model > Add Model Version.
- Clicked command + i then add version to modelOne.xcdatamodel inside modelOne.xcdatamodeld
- Modified modelOne.xcdatamodel, just added some attribute.
At the delegate I added the options dictionary to the NSPersistentStoreCoordinator, and I get the NSManagedObjectModel by merging the two models like:
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"modelOne" ofType:@"momd"];
NSURL *momURL1 = [NSURL fileURLWithPath:path1];
NSManagedObjectModel *modelOne = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL1]; NSString *path2 = [[NSBundle mainBundle] pathForResource:@"modelTwo" ofType:@"mom"];
NSURL *momURL2 = [NSURL fileURLWithPath:path2];
NSManagedObjectModel *modelTwo = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL2];
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel modelByMergingModels:[NSArray arrayWithObjects:modelOne, modelTwo, nil]];
最后我做了一个干净的构建,
Last I did a clean and build, I got error about 'Can't find model for source store'.
任何想法如何解决这个问题? (注意:如果我只是使用mergedModelFromBundles,我有另一个错误不能合并模型与两个不同的实体名为Entity1'
Any idea how I could fix this? (Note: if I just use mergedModelFromBundles, I got another error about 'Can't merge models with two different entities named Entity1'
推荐答案
p>我有一个类似的问题,原来是 .mom
编译的模型文件仍然在模拟器上的应用程序包中。显然,当你清理一个项目不会擦除模拟器上的应用程序包手动删除模拟器上的应用程序,然后清洗后再构建解决问题。
I had a similar problem. It turned out that the old .mom
complied model file was still in the app bundle on the simulator. Apparently, when you clean a project it doesn't wipe the app bundle on the simulator. Manually deleting the app off the simulator and then cleaning before building again solved the problem.
这篇关于多模型的iPhone核心数据迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!