GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: SwitData and NSEntityDescription error

Forums > SwiftUI

Attempting to use SwiftData and have run into an error that has me stumped.

BrentsMusicOffice crashed due to an uncaught exception NSInternalInconsistencyException. Reason: NSFetchRequest could not locate an NSEntityDescription for entity name 'Instrument'.

The app runs in the simulator correctly, but crashes when I go to the screen using the records of that model 'Instrument' in XCode when starting from ContentView.

I saw Paul's page about common swiftdata errors, but I believe I have the model listed in the modelContainer correctly:

var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: [
            StudentUser.self,
            Instrument.self
            Actor.self,
            MainActor.self
            ])
    }

There are lots of view files, so I hesistate to post all of those, perhaps someone has had a similar issue and can suggest where the error might be. Thanks!

   

Found the error... it was in the preview code for ContentView - I had only placed the modelContainer for StudentUser.self, and had not included Instrument.self there. Adding Instrument.self fixed it.

#Preview {
    let config = ModelConfiguration(isStoredInMemoryOnly: true)
    let container = try! ModelContainer(for: StudentUser.self, Instrument.self, configurations: config)

        return ContentView()
            .modelContainer(container)
}

   

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

Click to save your free spot now

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.