WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

Hacking with iOS: Swift UI / preview crashes for core data

Forums > Books

I'm going through the bookworm examples, and running into something unexpected: the preview always crashes as soon as you try to use a @FetchRequest variable in your view. Since the same code works fine when running into a simulator, I suspect there is some extra setup that needs to be done in ContentView_Previews ?

1      

It appears to be necessary to do this:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        return ContentView().environment(\.managedObjectContext, moc)
    }
}

That should probably be added to the book?

3      

Even that is not quite good enough: things still break if you try to save anything :(

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.