xcode 将旧的 CoreData 模型导入新项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9747289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:40:51  来源:igfitidea点击:

Importing old CoreData model into new project

iosxcodecore-data

提问by Kenny Wyland

I have an old Xcode project which contains a CoreData model (containing a version 1 and version 2 of the model). For several reasons I need to create a new Xcode project and transfer all of the code to the new project.

我有一个旧的 Xcode 项目,其中包含一个 CoreData 模型(包含模型的版本 1 和版本 2)。出于多种原因,我需要创建一个新的 Xcode 项目并将所有代码转移到新项目中。

How can I import/transfer my old CoreData model in such a way that this new binary will still be able to read, and potentially migrate, the existing CoreData stores which are on my existing users' iPhones and iPads out in the world? I worry that if I push a new version using this new project that my users will update their app to the newest version and then it'll crash because the model or model version numbers don't match.

我如何才能导入/传输我的旧 CoreData 模型,使这个新的二进制文件仍然能够读取并可能迁移我现有用户的 iPhone 和 iPad 上的现有 CoreData 存储?我担心如果我使用这个新项目推送一个新版本,我的用户会将他们的应用程序更新到最新版本,然后它会因为型号或型号版本号不匹配而崩溃。

I'm NOT talking about adding a new version to the data model within the same app. I understand that process. This is about moving/importing/etc an existing data model from an old project into a new project.

我不是在谈论在同一应用程序中向数据模型添加新版本。我理解那个过程。这是关于将现有数据模型从旧项目移动/导入/等到新项目。

Should I just copy the files over and add them to my project manually? Do I need to change things in my build settings to account for it?

我应该直接复制文件并手动将它们添加到我的项目中吗?我是否需要更改构建设置中的内容来解决这个问题?

采纳答案by Kenny Wyland

In the end, this is how I solved it:

最后,我是这样解决的:

  1. Create new project with CoreData
  2. Copy the raw CoreData model file over into my new project. Add it to the project.
  3. Delete the empty CoreData model autocreated by the new project.
  4. In the Project Settings, under Build Phases, Compile Sources, I added the copied CoreData model file.
  1. 使用 CoreData 创建新项目
  2. 将原始 CoreData 模型文件复制到我的新项目中。将其添加到项目中。
  3. 删除由新项目自动创建的空 CoreData 模型。
  4. 在项目设置的构建阶段、编译源下,我添加了复制的 CoreData 模型文件。

Then I used the code that Scott provided above:

然后我使用了 Scott 在上面提供的代码:

[NSManagedObjectModel mergedModelFromBundles:nil]

which automatically finds all of the models and combines them. By deleting the auto-generated one and adding my transferred one then everything works out just fine.

它会自动找到所有模型并将它们组合起来。通过删除自动生成的一个并添加我传输的一个然后一切正常。

回答by Scott Corscadden

As long as you keep the same application identifier, your new code will replace the binary for installed users while keeping all their data there untouched. So your new project essentially swaps in as a new binary. After that, it's up to you to make sure you load the right .sqlite file, handle the upgrades, etc.

只要您保持相同的应用程序标识符,您的新代码将替换已安装用户的二进制文件,同时保持他们的所有数据原封不动。所以你的新项目本质上是作为一个新的二进制文件交换的。之后,由您来确保加载正确的 .sqlite 文件、处理升级等。

Let me edit this a bit further. Downvote has a sad.

让我再编辑一下。Downvote 有一个悲伤。

There is a ZMETADATA table (or equivalent, can't get to that now) that has all the information necessary to identify things. Further there is hashes to know whether the current versions are present such that automatic migration can happen. Provided the hashes exist, and that you've loaded your model via [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]]instead of [NSManagedObjectModel mergedModelFromBundles:nil], then all should be well.

有一个 ZMETADATA 表(或等效的,现在无法获得),其中包含识别事物所需的所有信息。此外,还有哈希值可以了解当前版本是否存在,以便可以进行自动迁移。如果散列存在,并且您已经通过[[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]]而不是加载模型[NSManagedObjectModel mergedModelFromBundles:nil],那么一切都应该没问题。

回答by curt

I found a simpler way. I created a new project with core data and then closed it without building or running it. I then used an IDE to open the xcdatamodeld. A text editor would probably work just as well. I had to drill down to the content. This may be because I'm using PHPStorm and it's trying to make a project out of this. The file that I wanted to edit looked like this:

我找到了一个更简单的方法。我用核心数据创建了一个新项目,然后在没有构建或运行它的情况下关闭它。然后我使用 IDE 打开 xcdatamodeld。文本编辑器可能也能正常工作。我不得不深入研究内容。这可能是因为我正在使用 PHPStorm 并且它正试图用它来制作一个项目。我要编辑的文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0"   lastSavedToolsVersion="1" systemVersion="11A491" minimumToolsVersion="Automatic"   sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
</model>

I then opened the source xcdatamoleld and copied every thing between the model tags to the new file. I closed the files and built the project. I didn't copy the actual model data (.storedata).

然后我打开源 xcdatamoleld 并将模型标记之间的所有内容复制到新文件中。我关闭了文件并构建了项目。我没有复制实际的模型数据 (.storedata)。

One caveat: In my original project, I was constantly changing the model and deleting the model data. Xcode couldn't handle it and threw off various errors. The last time I did it, I got the warning:

一个警告:在我的原始项目中,我不断地更改模型并删除模型数据。Xcode 无法处理它并抛出各种错误。我上次这样做时,收到警告:

CoreData: warning: Unable to load class named 'Performance' for entity 'Performance'.  Class not found, using default NSManagedObject instead.

This warning reoccured in the new project so it's something wrong in the model definition. Fortunately, it doesn't cause any problems with the execution. If you don't have this warning, you may be fine.

这个警告在新项目中再次出现,所以模型定义有问题。幸运的是,它不会对执行造成任何问题。如果您没有收到此警告,则可能没问题。

This was with Xcode 8.2.1 and it was a Swift project.

这是使用 Xcode 8.2.1 的,它是一个 Swift 项目。