【问题标题】:How to solve Core Data error in AppDelegate?如何解决 AppDelegate 中的核心数据错误?
【发布时间】:2017-08-08 03:47:38
【问题描述】:

我使用 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 并知道他们所说的错误的典型原因是如何发生的?

【问题讨论】:

    标签: ios swift core-data


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      相关资源
      最近更新 更多