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

后端 未结 1 702
花落未央
花落未央 2020-12-17 04:06

I have created new version of CoreData model and migrated existing one to it. Application works without any issues on iOS 9+, but for iOS 9 and 10 I am getting this error:

相关标签:
1条回答
  • 2020-12-17 04:38

    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.

    0 讨论(0)
提交回复
热议问题