一尘不染

iOS 9、10 CoreData:无法在路径上加载优化模型

swift

我创建了CoreData模型的新版本,并将现有版本迁移到该模型。应用程序在iOS 9+上运行时没有任何问题,但是对于iOS 9和10,我收到此错误:

2017-10-22 19:28:37.081 CafeManager [16654:1918728] CoreData:无法在路径’/ Users /
dj-glock / Library / Developer / CoreSimulator / Devices /
A81AA9C4-7B59-4422-BA0A-0FD0D1A05205​​加载优化的模型/data/Containers/Bundle/Application/DD66571C-4EB6-4A8B-A99B-B447DD0FFFBA/CafeManager.app/CafeManager.momd/CafeManager
v2.omo’

iOS 10:

CoreData:注释:无法在路径’/ Users / dj-glock / Library / Developer / CoreSimulator /
Devices / 3708F142-3BD0-4C70-8515-217B7785D285 / data / Containers / Bundle
/ Application / 3842402F-BEAE-47CB上加载优化的模型-8C27-EC6CA7D76B03 /
CafeManager.app / CafeManager.momd / CafeManager v2.omo’

我检查了很多类似的问题,但是没有找到解决方案。我试图在模拟器上重新安装应用程序,以重新启动等等。有人可以建议吗?

文件夹包含以下文件:

在此处输入图片说明

适用于iOS 9的AppDelegate CoreData堆栈:

// MARK: - Core Data stack for iOS 8+
static var managedObjectContext: NSManagedObjectContext = {
    var applicationDocumentsDirectory: URL = {
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return urls[urls.count-1]
    }()

    var managedObjectModel: NSManagedObjectModel = {
        let modelURL = Bundle.main.url(forResource: "CafeManager", withExtension: "momd")!
        return NSManagedObjectModel(contentsOf: modelURL)!
    }()

    var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        let a = managedObjectModel.entities
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
        let url = applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
        print(url)
        do
        {
            let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
            try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
        } catch {
            NSLog("Error initializing smStore for iOS 8+ - \(error.localizedDescription)")
        }
        return coordinator
    }()
    let coordinator = persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

苹果开发者论坛


阅读 253

收藏
2020-07-07

共1个答案

一尘不染

在<iOS 11上,优化模型版本似乎是问题所在。只需改用未优化的 .mom 模型版本( CafeManager v2.mom )。

这是我解决的方法:

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
}
2020-07-07