NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Core Data Error Checking

Forums > Swift

Being a newbie to Swift/SwiftUI I am struggling to understand how to error check core data. I have searched the internet and haven't found anything on how to do this. I am a visual learner and I enjoy all the videos Paul @twostraws puts out that shows me how to implement Swift and SwiftUI.

When you create a new project in SwiftUI 2.0 you get this in the Persistence.swift file:

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)")
  1. My question is how do we check for these types of errors?

  2. How do we error check when performing our do/try catch blocks. I see fatalerror always being used but I'm told we shouldn't use it.

Apologize for the newbie questions, I am sure its quite basic, but its not obvious to me. So any help here would be greatly appeciated.

Thanks, Mark


Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.