3中的核心数据轻量级迁移无法正常工作

3中的核心数据轻量级迁移无法正常工作

本文介绍了Swift 3中的核心数据轻量级迁移无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 persistentContainer

lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */
     //   let container = NSPersistentContainer(name:"YiuPai")


       let container = NSPersistentContainer(name:"YiuPai")

        let storeUrl = self.applicationDocumentsDirectory.appendingPathComponent("YiuPai.sqlite")

        print(storeUrl)


        let description = NSPersistentStoreDescription(url: storeUrl)

        description.shouldInferMappingModelAutomatically = true
        description.shouldMigrateStoreAutomatically = true

        container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: storeUrl)]


        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

错误我要得到

2017-08-24 16:37:43.912800+0530 YiuPai[1688:496051] [error] error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite options:{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite, reason=Can't copy source store to destination store path, destinationURL=file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/.YiuPai.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, NSUnderlyingError=0x170851460 {Error Domain=NSSQLiteErrorDomain Code=5 "(null)" UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/.YiuPai.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, Source database Path=/var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite, reason=Failed to replace destination database}}} with userInfo dictionary {
    NSUnderlyingError = "Error Domain=NSSQLiteErrorDomain Code=5 \"(null)\" UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/.YiuPai.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, Source database Path=/var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite, reason=Failed to replace destination database}";
    destinationURL = "file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/.YiuPai.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3";
    reason = "Can't copy source store to destination store path";
    sourceURL = "file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite";
}

我该如何解决?

推荐答案

我认为我遇到了您的错误。尝试像这样设置storeUrl:

I think I had your error. Try settings your storeUrl like this:

let storeUrl = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!.appendingPathComponent("YiuPai.sqlite")

这篇关于Swift 3中的核心数据轻量级迁移无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 04:32