特定版本轻量级迁移后自定义代码执行
我在 Core Data 中有 2 个对象模型(比如 v1 和 v2)。此迁移符合轻量级迁移的条件。现在,我想在迁移后执行自定义代码,但仅限于从 v1 迁移到 v2 时。稍后如果我引入 v3,我不希望执行自定义代码。
有办法做到这一点吗?
提前致谢, 阿努帕姆
I have 2 object models in Core Data (say v1 and v2). This migration is eligible for light weight migration. Now, I want to execute custom code after the migration but only when the migration is from v1 to v2. Later on if I introduce v3, I don't want the custom code to get executed.
Is there a way to do this?
Thanks in advance,
Anupam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下是处理这种情况的方法,基本上是通过使用商店的元数据,正如 Apple 工程师在 WWDC 2010 上所建议的那样:
或多或少,类似于

Here is how to handle this situation, basically by using the store's metadata, as suggested by Apple engineers at WWDC 2010:
More or less, something like

可能有更好的方法,购买这个应该可行:
通过在核心数据中保留一个名为“Information”的实体并拥有一个名为“CoreDataVersion”的属性来跟踪数据库版本。
迁移代码完成后,添加代码以检查核心数据中的版本号。
如果“CoreDataVersion”的值为“v1”并且您的应用程序现在处于“v2”(这可以对每个版本进行硬编码),请执行其他自定义代码,然后将新版本写回数据库。
如果您已经向用户发布了“v1”,只需说如果数据库中没有“CoreDataVersion”,那么它就是“v1”。
There might be a better way, buy this should work:
Keep track of the DB version by keeping an entity in Core Data named "Information" and have a property named "CoreDataVersion".
After the migration code finishes add code to check for the version number in the core data.
If the value of "CoreDataVersion" is "v1" and your app is now at "v2" (this can be hardcoded with each version), execute the additional custom code and then write the new version back to the DB.
If you already have "v1" published to users, just say that if there is no "CoreDataVersion" in the DB, then it is "v1".
这就是您检索应用程序版本的方式
and this is how you retrieve apps version
在创建执行自动迁移的持久存储之前,请确定是否要迁移到版本 2。如果要迁移,请设置一个标志以在迁移发生后进行更改。
要确定要从中迁移的内容,请执行以下操作:
版本标识符在 Xcode 中设置。
Before you create the persistent store that does the automatic migration, determine if you'll be migrating to version 2. If you will be, set a flag to do your changes after the migration happens.
To determine what you'll be migrating from, do this:
The version identifiers are set in Xcode.
另一种方法是进行从 v1 到 v2 的自定义数据迁移。首先查看 http://www.timisted.net/blog/archive/核心数据迁移/。
Another way to do it is to do a custom data migration from v1 to v2. Start by looking over http://www.timisted.net/blog/archive/core-data-migration/.
另一个对我来说效果很好的解决方案:
Another solution that worked well for me: