Core Data - Failed to Load Optimized Model at Path

Core Data - Failed to load optimized model at path

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.

Sample Image

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.

CoreData: annotation: Failed to load optimized model at path (Xcode 11 - iOS 12)

I was able to solve the issue thanks to the hint in @ChaitanyaKhurana's comment above.

Here is the swift code I have implemented, replacing the original single line

let container = NSPersistentContainer(name: "ModelName")

Part of the code is needed to retrieve the model version string (from the .plist file located in the .momd package) in order to avoid having to update the code each time there is a new model version.

Please also note that the new/alternative code only executes on iOS versions prior to 13.0 as there is not issue on iOS 13.

let modelName = "ModelName"

var container: NSPersistentContainer!

if #available(iOS 13.0, *) {
container = NSPersistentContainer(name: modelName)
} else {
var modelURL = Bundle(for: type(of: self)).url(forResource: modelName, withExtension: "momd")!
let versionInfoURL = modelURL.appendingPathComponent("VersionInfo.plist")
if let versionInfoNSDictionary = NSDictionary(contentsOf: versionInfoURL),
let version = versionInfoNSDictionary.object(forKey: "NSManagedObjectModel_CurrentVersionName") as? String {
modelURL.appendPathComponent("\(version).mom")
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
container = NSPersistentContainer(name: modelName, managedObjectModel: managedObjectModel!)
} else {
//fall back solution; runs fine despite "Failed to load optimized model" warning
container = NSPersistentContainer(name: modelName)
}
}

iOS 9, 10 CoreData: Failed to load optimized model at path

It seems to be the problem with optimized model versions on < iOS 11. Just use unoptimized .mom model version instead (CafeManager v2.mom).

Here's how I fixed it:

public func managedObjectModel() -> NSManagedObjectModel {
let omoURL = modelBundle.url(forResource: name, withExtension: "omo", subdirectory: modelDirectoryName)
let momURL = modelBundle.url(forResource: name, withExtension: "mom", subdirectory: modelDirectoryName)
guard var url = omoURL ?? momURL else { fatalError("model version \(self) not found") }
// Use unoptimized model version < iOS 11
if #available(iOS 11, *) {} else { if let momURL = momURL { url = momURL} }
guard let model = NSManagedObjectModel(contentsOf: url) else { fatalError("cannot open model at \(url)") }
return model
}

If you're thinking, I want my speed, why would I use something unoptimized, read this answer.

CoreData: annotation: Failed to load optimized model at path '/var/containers/Bundle/ .... '

So, after a few days I have managed to solve it. I'm not familiar enough with the insides of Xcode, but all I had to do was rewrite the CoreData Model. Best to make a back up before trying this!

  1. Take a screenshot of current attributes and delete the CoreDataModel ( .xcdatamodeld file)
  2. Add a new file to project (Data Model template)
  3. Write old attributes
  4. Recompile and build

My assumption would be that the problem was caused after migrating a project started in Xcode 8 (written in Swift 3) to Xcode 9 (upgraded to Swift 4). It had no problem with devices running iOS 11 (same upgrade level as Xcode 9), but found it to consistently cause lag and eventually crashes in older iOS versions.

I don't understand why simply modifying file properties did not fix it, but I'm happy to have fixed it and move on.

CoreData: annotation: Failed to load optimized model at path 'Users/~/GoogleMaps.bundle/GMSCacheStorage.momd/StorageWithTileProto.omo'

I just spent 5 hours with the same problem. Sometimes the map would show up and sometimes it would not. In the end, I found out I had a problem with my key. I accidentally deleted its permission to iOS. Some emulators were showing the map because they had the key cached. And some others were displaying the "StorageWithTileProto" error, because they had the tiles cached with the old key but no authorization do show it.
I would recommend anyone with the "StorageWithTileProto" to test it with a new unrestricted key and to fully delete the app from the device/emulator before testing it. This might solve your problem as well.



Related Topics



Leave a reply



Submit