草庐IT

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

coder 2023-09-24 原文

如果我已经发布的 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 - 如果我已经发布的 v1 没有版本化的核心数据模型,我可以使用 "Automatic Lightweight Migration"吗?的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  7. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  8. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  9. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  10. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

随机推荐