多型号 iPhone Core 数据迁移

发布于 2024-09-10 01:02:57 字数 1117 浏览 0 评论 0原文

我的项目中有两个模型,我想允许其中之一的版本。这就是我所做的:

  • 选择 modelOne.xcdatamodel 然后 Design >数据模型>添加模型版本。
  • 单击命令+ i,然后将版本添加到 modelOne.xcdatamodel 内的 modelOne.xcdatamodeld
  • 修改 modelOne.xcdatamodel,只是添加一些属性。
  • 在委托中,我将选项字典添加到 NSPersistentStoreCoordinator 中,并通过合并两个模型来获取 NSManagedObjectModel,如下所示:

    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]];
    
  • 最后我进行了清理和构建,我收到了有关“Can”的错误t 找到源存储的模型'。

知道我该如何解决这个问题吗? (注意:如果我只使用 mergedModelFromBundles,我会收到另一个关于“无法将模型与名为 Entity1 的两个不同实体合并”的错误

I have two models at my project, I want to allow versions to one of them. Here's what I did:

  • 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'.

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-09-17 01:02:57

我有类似的问题。结果发现旧的 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文