NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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



There are a few questions in this forum re: Core Data Error Handling. I would appreciate it if a more experienced developer with CD provided some insight but I do have an initial response in the hopes that someone will chime in and correct or add to my comments below.

As I understand it, Inside creating this stack the fatal error is there to help you in development only to get it set up correctly. We want the app to crash if it hits this error. Once you have the stack set up to your satisfaction/ configuration then you know this error should not happen in production.

I am more concerned right now with testing my CRUD operations with CD and CloudKit. What I have found with using CD in comparison to just using CloudKit with no local persistence is that getting this stack set up correctly handles a lot of the errors that I typically was concerned with in CloudKit.

For example, saving to a local store with the NSPersistentCloudKitContainer set up correctly tends to handle most networking and CloudKit errors as CD handles them on the back end.

That has not solved a few problems for me though. Even if everything is configured and setup correctly you might have a user download the app that does not have iCloud Drive enabled or has maxed out their iCloud Storage data. In these cases you will still have a working app but data will not be in the cloud and therefore will not sync with other devices.

I can check for 'iCloud Drive on' easy enough before performing an operation but have not figured out where these errors express themselves in the Core data error handling as that is handled typically back end. It might save to the local store correctly avoiding a throw on your save operation but it is not clear if it is providing any kind of feedback on iCloud Storage being exceeded.

I hope this helps and look forward to seeing other responses. Thanks


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.