Core Data - Failed to load optimized model at path

IosObjective CXcodeCore DataIos9

Ios Problem Overview


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:

> CoreData: Failed to load optimized model at path '/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?

Ios Solutions


Solution 1 - Ios

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.

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.

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

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.

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.

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.

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.

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.

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.

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.

Solution 2 - Ios

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.

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

// 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.

So it might be a pod-issue.

Solution 3 - Ios

> 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
    
 ...

Solution 4 - Ios

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.

Solution 5 - Ios

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 momd file in the wrong bundle.

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

'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.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlauco NevesView Question on Stackoverflow
Solution 1 - IosdigitalHoundView Answer on Stackoverflow
Solution 2 - IosDiAvisooView Answer on Stackoverflow
Solution 3 - IosSiempayView Answer on Stackoverflow
Solution 4 - IosMaheshView Answer on Stackoverflow
Solution 5 - IosReDetectionView Answer on Stackoverflow