iphone - 如果我已经发布的 v1 没有版本化的核心数据模型,我可以使用 "Automatic Lightweight Migration"吗?

标签 iphone ios core-data xcode4 migration

如果我已经发布的 v1 没有版本化的核心数据模型,我可以使用“自动轻量级迁移”吗?

如果是,我需要应用的记录步骤是否有任何更改?

最佳答案

您不仅可以做到这一点,从某种意义上说,这是您可以做到这一点的唯一方法。来自Apple Documentation , "要创建版本化模型,您需要从普通模型开始..."

你的 v1 有一个正常的模型。只要您拥有该模型,并按照该教程中链接的步骤创建版本化模型,轻量级迁移就会起作用——如果您的迁移满足通常的轻量级迁移要求。轻量级迁移发生在您的 v2 应用程序(或 v1.1 或其他)中。您的 v1 应用程序中的数据模型基本上没有任何相关性。 Core Data 需要的是找到新的 v2 应用程序具有与本地设备上的 Core Data 存储中找到的内容相匹配的数据模型副本,并且具有描述您希望如何存储数据的新数据模型从这一点开始。如果所需的更改满足轻量级迁移的要求,则执行。

这些要求是什么?来自 the Apple documentation on Lightweight Migration :

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.

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

• 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 的这些功能还为时不晚。 :) 要回答您的具体问题,您无需对文档中概述的标准步骤进行任何更改。

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

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 对核心数据模型进行了更改,而没有先创建版本化数据模型。这是完全不同的事情。

如果是这样的话,那你的工作就更难了。但是,您可以检索您需要的东西,前提是您保留源代码的备份或在像 git 这样的存储库中管理您的代码(我建议所有开发人员都这样做)。如果您已经更改了 v2 的核心数据模型,您需要做的是将当前数据模型转换为版本化模型,然后恢复/ checkout 应用程序的 v1 副本,复制核心数据模型(*. xcdatamodel 文件)从那里复制到您当前的项目中,这样您就可以同时拥有 v1 数据模型和更新的数据模型。然后,您将有可能使用轻量级迁移,如上所述。

请注意,这里的关键问题是您何时更改了数据模型。您的应用程序称为 v1 还是 v2 本质上与问题无关,除了很明显您可能在将应用程序的版本号更改为 v2 的同时对数据模型进行了更改。

关于iphone - 如果我已经发布的 v1 没有版本化的核心数据模型,我可以使用 "Automatic Lightweight Migration"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7497284/

相关文章:

iphone - 可以在 iPhone 上串流 mms、ASX、RTSP 流吗?

iphone - UDP sendto() 没有可用的缓冲区空间

ios - 内存警告导致崩溃

ios - 使用全局托管对象上下文来支持并发

swift - 自引用实体,为了与 NSFetchedResultsController 建立父子关系

不同 iPhone 的 iOS 图像尺寸指南

iphone - ImageView 在设备方向改变时消失 - iPhone - Objective c

cocoa - 核心数据迁移: exceptions after changing a relationship from one entity to its parent entity

iphone - 如何在 obj-c 上合成 C 数组的 getter 和 setter

ios - 如何使 collectionview 响应其自身 View 之外的平移手势