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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.