xcdatamodel的多个版本是否意味着我们需要多个xcmappingmodel文件?

发布于 2025-01-03 02:35:51 字数 358 浏览 4 评论 0原文

我有多个版本的 xcdatamodel 文件:

app1.0.xcdatamodel
app1.1.xcdatamodel
app1.2.xcdatamodel (current)

这是否意味着我需要 xcmappingmodel 文件的多个组合来涵盖所有升级方案?

app1.0_to_app1.1.xcmappingmodel (had this already)
app1.1_to_app1.2.xcmappingmodel (is it iterative?)
app1.0_to_app1.2.xcmappingmodel (too much?)

谢谢!

I have multiple versions of xcdatamodel files:

app1.0.xcdatamodel
app1.1.xcdatamodel
app1.2.xcdatamodel (current)

Does this mean I need multiple combinations of xcmappingmodel files to cover all upgrade scenarios?

app1.0_to_app1.1.xcmappingmodel (had this already)
app1.1_to_app1.2.xcmappingmodel (is it iterative?)
app1.0_to_app1.2.xcmappingmodel (too much?)

Thanks!

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

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

发布评论

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

评论(3

郁金香雨 2025-01-10 02:35:52

progressivelyMigrateURL 是一个很好的示例,但我认为您实际上并不需要它,因为只要您开发应用程序,就会出现文档的版本,因此每次您需要的映射模型数量与数据模型的受支持版本减一而不是更多(例如,您不需要 app1.0_to_app1.1.xcmappingmodel,因为 1.1 版本不再是最新版本)。每次创建新版本时,您只需要更正您拥有的每个映射模型中的目标模型,并在需要时添加一个目标模型,也许您需要生成新版本并删除旧版本。事实上,您可能会注意到,一个阶段的迁移(与渐进式迁移相比,不会强迫您创建更多映射模型)在运行时要快得多。
您也不需要为琐碎的情况创建映射模型,也不需要使用轻量级迁移(如果具体情况需要无法在运行时生成的映射模型,请使用默认迁移过程(当然您需要将其放在您的应用程序中) Bundle)) 或借助 NSMappingModel 类的 inferredMappingModelForSourceModel:destinationModel:error: 方法在运行时创建的映射模型进行迁移,然后根据需要在代码中进行自定义(您将需要触发据我所知,在这种情况下,通过调用 NSMigrationManager 实例的 migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error: 方法手动迁移)。
祝你好运!

The progressivelyMigrateURL is a great sample, but I don't think you actually need it as versions of your document appear as long as you develop the application so every time you need as many mapping models as the count of supported versions of the data model minus one and not more (for example you don't need the app1.0_to_app1.1.xcmappingmodel as 1.1 version is not the latest version any more). Every time when you create a new version you need just to correct a target model in every mapping model you have and add one more if needed, maybe you will need to generate new ones and remove old ones though. The fact is that migration in one stage (which doesn't force you to create more mapping models compared to the progressive one) is much much faster in runtime as you may notice.
You also don't need to create mapping models for trivial cases and either use a Lightweight Migration (use The Default Migration Process instead if concrete situation needs a mapping model which cannot be generated in runtime (of course you need to have it in your app bundle)) or migrate with a help of the mapping model created in runtime with the help of the inferredMappingModelForSourceModel:destinationModel:error: method of the NSMappingModel class and then customized in code if needed (you will need to trigger migration manually in this case by calling the migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error: method of NSMigrationManager instance as far as I understand).
Good luck!

云巢 2025-01-10 02:35:51

Core Data 要求您创建一个映射模型,以从数据存储的当前版本到数据存储的最新版本。这意味着您需要制作一个从 v1 -> v1 开始的版本。 v2 和 v2 -> v3 和 v1 -> v3。

来自核心数据版本控制和迁移指南

尝试找到从托管对象模型映射的映射模型
将现有存储更改为持久存储正在使用的存储
协调员。核心数据搜索应用程序的资源
获取可用的映射模型并依次测试每个模型。如果找不到
一个合适的映射,Core Data 返回 NO 和一个合适的错误。

请注意,您必须创建合适的映射模型才能
此阶段才能成功。

正如本 Apple 文档中所讨论的
核心数据映射

Core Data requires that you create a mapping model to go from the current version of the data store to the latest version of the data store. This means that you will need to make one that goes from v1 -> v2 and v2 -> v3 and v1 -> v3.

From Core Data Versioning and Migration Guide

Tries to find a mapping model that maps from the managed object model
for the existing store to that in use by the persistent store
coordinator. Core Data searches through your application’s resources
for available mapping models and tests each in turn. If it cannot find
a suitable mapping, Core Data returns NO and a suitable error.

Note that you must have created a suitable mapping model in order for
this phase to succeed.

As discussed in this Apple Document
Core Data Mapping

纸短情长 2025-01-10 02:35:51

您可以实施渐进式数据迁移。在此处查找 progressivelyMigrateURL http://media .pragprog.com/titles/mzcd/code/ProgressiveMigration/AppDelegate.m

You could implement progressive data migration. Look for progressivelyMigrateURL in here http://media.pragprog.com/titles/mzcd/code/ProgressiveMigration/AppDelegate.m

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