2017-08-07 78 views
0

我用Core Data創建了一個應用程序。在AppDelegate中我有這樣的代碼:如何解決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和知道一個錯誤的典型原因如何,他們談論能發生?

回答

0

因此,您可以刪除或註釋fatalError("Unresolved error \(error), \(error.userInfo)")代碼行。如果您閱讀註釋中的第一行「用代碼替換此實現來正確處理錯誤」,它會告訴我們我們打算使用我們自己的實現,fatalError()方法用於您的開發目的,出錯了。

通常是由於項目配置錯誤或天堂禁止在版本之間遷移失敗而導致的。在這些情況下,您希望在項目啓用之前捕獲並修復這些問題,但如果由於設備用完存儲空間或權限問題而發生錯誤,最好只觀察此錯誤,並在發生任何問題時通知用戶通過警報。