ios 核心数据 - 无法在路径加载优化模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32233939/
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-08-31 07:24:36  来源:igfitidea点击:

Core Data - Failed to load optimized model at path

iosobjective-cxcodecore-dataios9

提问by Glauco Neves

I'm getting some of these prints in my console while running my application from Xcode 6 in my iPhone 6 with iOS 9 beta 5:

在使用 iOS 9 beta 5 的 iPhone 6 中从 Xcode 6 运行我的应用程序时,我在控制台中获得了其中一些打印:

CoreData: Failed to load optimized model at path '/var/mobile/Containers/Bundle/Application/0000000B-BDBC-0000-000B-0000FB00000B/Distribution.app/database.momd/database.omo'

CoreData:无法在路径“/var/mobile/Containers/Bundle/Application/0000000B-BDBC-0000-000B-0000FB00000B/Distribution.app/database.momd/database.omo”加载优化模型

I cound't find something related to this, does anyone have some clue about this message?

我找不到与此相关的内容,有人对此消息有一些线索吗?

采纳答案by digitalHound

I've run into this issue and did some digging around.

我遇到了这个问题并进行了一些挖掘。

I've been building with Xcode 6.4 and it looks like previously core data only produced a .mom file in the MyApp.ipa momd directory. This screenshot is from a project that has seen several version of Xcode.

我一直在用 Xcode 6.4 构建,看起来以前的核心数据只在 MyApp.ipa momd 目录中生成了一个 .mom 文件。此屏幕截图来自一个已经看到多个 Xcode 版本的项目。

Notice all the older model versions only have a .mom file. I just created a new model version today and it has both a .mom and an .omo file.

请注意,所有旧型号版本只有一个 .mom 文件。我今天刚刚创建了一个新的模型版本,它有一个 .mom 和一个 .omo 文件。

enter image description here

在此处输入图片说明

It appears that Xcode 6.4 (and perhaps some of the beta 7.x versions as well) do not know how to load the optimized version of the data model because I also get the

Xcode 6.4(也许还有一些 beta 7.x 版本)似乎不知道如何加载数据模型的优化版本,因为我也得到了

2015-10-16 11:11:42.563 MyAppName[1767:599635] CoreData: Failed to load optimized model at path '/var/mobile/Containers/Bundle/Application/D887D60B-FB28-4059-8167-F573460D98F8/MyAppName.app/MyDataModel.momd/MyDataModel3_0Analytics.omo'

warning when compiling with 6.4. However, when compiling the app with the latest app store version of Xcode (7.0.1) I do not get that warning. I'm guessing that the reason Mahesh's solution is working is because re-writing the entire schema creates the .omo file the app is looking for in the app bundle.

使用 6.4 编译时发出警告。但是,当使用最新的应用程序商店版本的 Xcode (7.0.1) 编译应用程序时,我没有收到该警告。我猜测 Mahesh 的解决方案起作用的原因是因为重写整个架构会创建应用程序在应用程序包中寻找的 .omo 文件。

The solution for me was to generate a new data model version in core data and then build with Xcode 7. It seems that creating a new model version creates the optimized model file. In my testing though, even with this file created Xcode 6.4 still throws the error. It wasn't till I tried it with Xcode 7.0.1 that the warning went away.

我的解决方案是在核心数据中生成一个新的数据模型版本,然后使用 Xcode 7 构建。似乎创建一个新的模型版本会创建优化的模型文件。不过,在我的测试中,即使创建了这个文件,Xcode 6.4 仍然会引发错误。直到我用 Xcode 7.0.1 尝试它,警告才消失。

This is speculation but I think if you have an existing project and have not created a new data model version and build with Xcode 7 that the .omo file is missing, so it's throwing the warning because it cannot find the file. However if you've versioned your data model and build with Xcode 6.4 it seems that the earlier Xcode version doesn't do something correctly with the optimized version and it doesn't load it even if its there. These are just my observations though.

这是推测,但我认为如果您有一个现有项目并且尚未创建新的数据模型版本并使用 Xcode 7 构建 .omo 文件丢失,因此它会抛出警告,因为它找不到该文件。但是,如果您对数据模型进行了版本控制并使用 Xcode 6.4 进行构建,那么较早的 Xcode 版本似乎无法对优化版本正确执行某些操作,并且即使它在那里也不会加载它。不过这些只是我的观察。

I verified that I had an optimized model (.omo file) to load by doing the following: 1. archive your project 2. change the .ipa extension to .zip 3. expand your zip file 4. click on the "payload" folder and right click (or cmd click) on the app bundle in the folder and select "Show Package Contents". 5. click on the .momd directory, you should see all of your available managed object models there.

我通过执行以下操作验证了我有一个优化的模型(.omo 文件)要加载: 1. 存档您的项目 2. 将 .ipa 扩展名更改为 .zip 3. 展开您的 zip 文件 4. 单击“payload”文件夹并右键单击(或 cmd 单击)文件夹中的应用程序包,然后选择“显示包内容”。5. 单击 .momd 目录,您应该在那里看到所有可用的托管对象模型。

If all you have is .mom files and no .omo files then the warning makes complete sense, the app is unable to open a file that does not exist.

如果您拥有的只是 .mom 文件而没有 .omo 文件,则警告完全有意义,应用程序无法打开不存在的文件。

In my testing it seems like the warning was informational only. I never had any crashing because of it. It seems like core data may try to load the optimized model first, and if that fails fall back to the regular .momd model. This is merely my speculation though.

在我的测试中,警告似乎只是提供信息。我从来没有因为它而崩溃。似乎核心数据可能会首先尝试加载优化的模型,如果加载失败,则返回到常规的 .momd 模型。不过这只是我的推测。

I'm not sure if everything here is entirely correct, this is just what I've observed so far in trying to debug this. If anyone else can contribute any more info I welcome your input.

我不确定这里的一切是否完全正确,这正是我迄今为止在尝试调试时观察到的。如果其他人可以提供更多信息,我欢迎您的意见。

回答by DiAvisoo

I ran into this problem this morning. Did a small hack to get it running. I think it's got something to do with versioning mismatch but I'm not sure.

我今天早上遇到了这个问题。做了一个小技巧来让它运行。我认为这与版本不匹配有关,但我不确定。

Anyhow, if you're loading a momd-file, just append a "/[filename].mom" to the NSURL to get it working.

无论如何,如果您正在加载一个 momd 文件,只需将“/[filename].mom”附加到 NSURL 即可使其工作。

In my case, I was loading the file Countly.momd and ended up doing this:

就我而言,我正在加载文件 Countly.momd 并最终这样做:

// Original loading
NSURL modelURL = [[NSBundle bundleForClass:[CountlyDB class]] URLForResource:@"Countly" withExtension:@"momd"];

// Small hack
modelURL = [modelURL URLByAppendingPathComponent:@"Countly.mom"];

Update: I was using a POD that used CoreData. Removing the pod and adding the source etc. from repo directly made the problem go away.

更新:我使用的是使用 CoreData 的 POD。直接从 repo 中删除 pod 并添加源等使问题消失。

So it might be a pod-issue.

所以这可能是一个 pod 问题。

回答by Mahesh

I found a solution for this. I rewrote the whole schema, and when I run the code I got rid of those warnings from core data.

我为此找到了解决方案。我重写了整个架构,当我运行代码时,我摆脱了核心数据中的那些警告。

I suggest please take back-up before you try this.

我建议在你尝试这个之前请先备份。

Hope it helps you.

希望对你有帮助。

回答by brahimm

After long search, it was just like this:

找了半天,原来是这样的:

public lazy var persistentContainer: NSPersistentContainer = {

    var modelURL = Bundle(for: type(of: self)).url(forResource: "Model", withExtension: "momd")!

    // ===> here you append the nameOfVersion.mom you created
    modelURL.appendPathComponent("Model 2.mom")
    let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
    let container = NSPersistentContainer(name: "ModelFile", managedObjectModel: managedObjectModel!)

    container.persistentStoreDescriptions.first?.shouldInferMappingModelAutomatically = false

 ...

回答by ReDetection

I want to answer to people who ran into this when was writing own pod which has own CoreData models. Probably, you've put your model definition to the bundle (that's good), but you search for the momdfile in the wrong bundle.

我想回答在编写自己的具有 CoreData 模型的 pod 时遇到这个问题的人。可能您已经将模型定义放入包中(很好),但是您momd在错误的包中搜索了该文件。

Let's say you have defined your bundle in podspec like this:

假设您已经在 podspec 中定义了您的包,如下所示:

'MYPodBundle' => [
    'Model/*.{xcdatamodeld,xcdatamodel}'
]

Then you should first find this bundle, and then locate your model inside it.

那么你应该首先找到这个包,然后在里面找到你的模型。

NSURL *bundleURL = [[NSBundle bundleForClass:[MYEntity class]] URLForResource:@"MYPodBundle" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
NSString *modelPath = [bundle pathForResource:@"MYCoreDataModel" ofType:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];

So you can continue creating CoreData stack.

所以你可以继续创建 CoreData 堆栈。

//This may be a bit offtopic because you weren't writing own pod, but your answer is on google's top.

//这可能有点题外话,因为你没有写自己的 pod,但你的答案在 google 的顶部。