UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Are there any best practices for what to do when CoreData loadPersistentStores fails?

Forums > iOS

I've gotten a couple crash reports that my persistent store is failing to load in my widget. Naturally, I haven't been able to reproduce this crash and I'm pretty new to using CoreData. What is the best way to handle a persistent store failing to load?

In this article, it shows:

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "CoreDataModelNameHere")
    container.loadPersistentStores { description, error in
        if let error = error {
            // Add your error UI here
        }
    }
    return container
}()

Should I show an alert or something telling the user their data couldn't be loaded and ask them to restart the app? Is there a good way of retrying? What about a widget, should I replace the widget view with an error message, or leave it in it's placeholder state?

Thanks in advance for any help!

Bonus question: What would cause the persistent store to fail loading?

Some more potentially helpful information: My store is in an app group so that I can access it from my app, the widgets, and some intents.

3      

It can faily only due to logical error as far as I know. No point in informing the user.

As for the widget it looks like it cannot access the DB file. You need to use App Groups and save files in special location in order to access them outside the main app

3      

Thanks for the response. My store file is already in an App Group. I think I will just have to add some more logging to get better more on the error.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.