如何解决AppDelegate中的Core Data错误?

时间:2017-08-07 17:05:01

标签: ios swift core-data

我使用Core Data创建了一个应用程序。在AppDelegate中我有这段代码:

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: "Teste")
        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
    }()

我想删除fatalError并知道他们谈论的错误的典型原因是怎样的?

1 个答案:

答案 0 :(得分:0)

因此,您可以删除或注释掉fatalError("Unresolved error \(error), \(error.userInfo)")代码行。如果您阅读注释中的第一行“将此实现替换为代码以正确处理错误”,它告诉我们我们打算使用我们自己的实现,fatalError()方法是为了您的开发目的而得到通知在调试中出现问题。

通常由项目配置错误引起,或天堂禁止在版本之间迁移失败。在这些情况下,您希望在项目上线之前捕获并修复这些问题,但如果由于设备的存储空间不足或权限问题而发生错误,最好只是观察此错误以及何时发生通知用户通过警报。