我可以使用“自动轻量级迁移”如果我已经发布v1没有版本化的核心数据模型? [英] Can I use "Automatic Lightweight Migration" if my already release v1 didn't have a versioned Core Data model?

查看:177
本文介绍了我可以使用“自动轻量级迁移”如果我已经发布v1没有版本化的核心数据模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我已经发布的版本v1没有版本化的Core Data模型,我可以使用自动轻量级迁移吗?

Can I use "Automatic Lightweight Migration" if my already release v1 didn't have a versioned Core Data model?

如果是,

推荐答案

不仅可以做到这一点,在某种意义上它是唯一的方法做这个。从 Apple文档,要创建版本化模型,请从普通模型开始...

Not only can you do this, in one sense it is the only way you can do this. From the Apple Documentation, "To create a versioned model, you start with a normal model..."

您的v1有一个正常的模型。只要您拥有该模型,并且按照本教程中链接的步骤创建版本化模型,轻量级迁移将有效 - 如果迁移满足常规轻量级迁移要求。轻量级迁移发生在您的v2应用程序(或在v1.1或任何其他)。您的v1应用程序中的数据模型基本上没有相关性。 Core Data所需要的是找到新的v2应用程序具有与本地设备上的Core Data存储中找到的数据模型相匹配的数据模型的副本,并且具有描述您希望数据存储方式的新数据模型从这一点向前。如果所需的更改符合轻量级迁移的要求,那么它会这样做。

Your v1 had a normal model. As long as you have that model, and you follow the steps linked in that tutorial to create a versioned model, lightweight migration will work—if your migration meets the usual lightweight migration requirements. The lightweight migration happens in your v2 app (or in v1.1 or whatever). The data model that was in your v1 app essentially has no relevance. What Core Data needs is to find that the new v2 app has a copy of the data model that matches what is found in the Core Data store on the local device, and has a new data model that describes how you want the data to be stored from this point forward. If the changes required meet the requirements for lightweight migration, it then does it.

这些要求是什么?从 Apple文档轻量级迁移


要执行轻量级迁移,Core Data需要能够找到
源和目标管理对象模型本身在运行时。
(Core Data搜索由NSBundle的allBundles和
allFrameworks方法返回的bundle)。然后它必须分析对
持久化实体和属性的模式更改,并生成一个推断的映射
模型。要使Core Data能够执行此操作,更改必须符合
明显的迁移模式,例如:

To perform a lightweight migration, Core Data needs to be able to find the source and destination managed object models itself at runtime. (Core Data searches the bundles returned by NSBundle’s allBundles and allFrameworks methods.) It must then analyze the schema changes to persistent entities and properties and generate an inferred mapping model. For Core Data to be able to do this, the changes must fit an obvious migration pattern, for example:

•简单添加一个新属性

•非可选属性变为可选

•可选属性变为非可选属性,并定义一个
默认值。

• Simple addition of a new attribute
• A non-optional attribute becoming optional
• An optional attribute becoming non-optional, and defining a default value.

如果您重命名实体或属性,则可以将目标模型中的
重命名标识符设置为
对应属性或实体的名称在源模型中。通常,
使用Xcode数据建模工具设置重命名标识符(对于
,NSEntityDescription或NSPropertyDescription对象)。在
Xcode中,重命名标识符位于Detail
Pane的用户信息窗格中,位于版本哈希修改器下方(请参阅Xcode中的浏览器视图
Core Data的工具)。您还可以使用
setRenamingIdentifier:在运行时设置标识符。例如,处理

If you rename an entity or property, you can set the renaming identifier in the destination model to the name of the corresponding property or entity in the source model. You typically set the renaming identifier using the Xcode Data Modeling tool, (for either an NSEntityDescription or an NSPropertyDescription object). In Xcode, the renaming identifier is in the User Info pane of the Detail Pane, below the version hash modifier (see The Browser View in Xcode Tools for Core Data). You can also set the identifier at runtime using setRenamingIdentifier:. For example, to handle

•将实体重命名为汽车,

•并重命名汽车的颜色
属性为paintColor

• Renaming of an entity Car to Automobile,
• and Renaming the Car’s color attribute to paintColor


加载目标数据模型之后,在尝试打开
存储文件之前,您将包含以下代码:

you would include the following code after loading the destination data model, and before attempting to open a store file:

NSEntityDescription *automobile = [[destinationModel entitiesByName] objectForKey:@"Automobile"];
[automobile setRenamingIdentifier:@"Car"];
NSPropertyDescription *paintColor = [[automobile attributesByName] objectForKey:@"paintColor"];
[paintColor setRenamingIdentifier:@"color"];


总之,你没有错过船,没有太晚,不能使用Core Data的这些功能。 :)为了回答您的具体问题,您无需从文档中列出的标准步骤进行更改。

In summary, you didn't miss the boat, and it's not too late to make use of these features of Core Data. :) And to answer your specific question, there is nothing you need to change from the standard steps outlined in the documentation.

稍后更新
根据您对另一个答案的评论,进一步的想法,您在哪里说:

Later update Further thoughts based on your comment to another answer, where you said:


只是为了确认,我不必在XCode后面跟踪我的核心数据
模型到什么看起来像一个v1然后版本呢?所以我可以
只是在我的
应用程序在v2时创建第一个核心数据模型版本。

so just to confirm, I don't have to in XCode back track my core data model to what it looked like a v1 and then version it then? So I can just create the first core data model version at the point where my application is at v2?

从你在这里说的,这个问题似乎不同于初始问题。您最初的问题是,您已经发布了v1的应用,而没有明确添加版本模型。但是,此语句意味着您已更改应用程序的v2的核心数据模型,而无需首先创建版本化数据模型。这是一个完全不同的事情。

From what you are saying here, the issue would appear to be different to the initial question. Your initial question says that you have already released v1 of your app, without explicitly adding a versioned model. However, this statement implies that you have made changes to your core data model for v2 of your app, without first creating a versioned data model. This is quite a different thing.

如果是这样,那么你的工作就更难了。然而,你可以检索你需要的东西,假设你保留源代码的备份或管理你的代码在一个仓库,如git(我建议所有开发人员做这两个)。如果您已经更改了v2的核心数据模型,则您需要做的是将当前数据模型转换为版本控制模型,然后恢复/检出您的应用的v1副本,复制核心数据模型(*。 xcdatamodel文件)从那里到你当前的项目,这样你就有了v1数据模型和你的较新的一个。然后,您将可以使用轻量级迁移,如上所述。

If this is the case, then your job is more difficult. However, you can retrieve what you need presuming you keep backups of your source code or manage your code in a repository like git (and I would recommend all developers do both). If you've already changed your core data model for v2, what you need to do is turn the current data model into a versioned model, then restore/checkout a copy of v1 of your app, copy the core data model (the *.xcdatamodel file) from there into your current project, so that you then have both the v1 data model and your newer one. Then you will potentially be able to use lightweight migration, as discussed above.

请注意,这里的关键问题是当您更改数据模型时。无论你的应用程序被称为v1还是v2本质上与这个问题无关,除了显然,你可能是在数据模型引入更改,同时您将版本号更改为v2的应用程序。

Note that the key issue here is when you changed your data model. Whether your app is called v1 or v2 is essentially irrelevant to the question, other than obviously that it may be that you introduced changes to the data model at the same time you changed the version number to v2 of the app.

这篇关于我可以使用“自动轻量级迁移”如果我已经发布v1没有版本化的核心数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆