How to handle errors in NSPersistentContainer.loadPersistentStores?

My AppDelegate came with the following all familiar Core Data template:

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "newsapp")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

He also had the following comment:

Permanent container for the application. This implementation creates and returns a container, loading storage for the application to it. This property is optional because there are legitimate ones that can lead to a failure in creating the repository.

Replace this implementation with code to handle the error correctly. fatalError()forces the application to generate a crash log and shut down. You should not use this feature in a delivery application, although this may be useful in development.

The following are typical causes of the error:

  • The parent directory does not exist, cannot be created or is not writable.
  • - .
  • .
  • .

, , .

, - , , , - , , . .

, ? , , , ?

, .

+4
1

fatalError "", , , , . , , .

- , . - , , . fatalError , - .

, , , , - , . . - , , . , iOS , , .

, , fatalError .

+2

Source: https://habr.com/ru/post/1683858/


All Articles