0

I am trying to use @FetchRequest to fetch all records from the SQLite database through CoreData. I have setup CoreData stack as shown below:

class CoreDataManager {
    
    private let persistentContainer: NSPersistentContainer
    static let shared = CoreDataManager()
    
    var context: NSManagedObjectContext {
        return persistentContainer.viewContext
    }
    
    private init() {
        persistentContainer = NSPersistentContainer(name: "FooModel")
        persistentContainer.loadPersistentStores { description, error in
            if let error = error {
                print("Unable to initialize Core Data \(error)")
            }
        }
    }
    
}


@main
struct FooAppApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView().environment(\.managedObjectContext, CoreDataManager.shared.context)
        }
    }
}

When I try to access it is my ContentView as shown below:

struct ContentView: View {
    
    @FetchRequest(entity: MyList.entity(), sortDescriptors: []) var myLists: FetchedResults<MyList>
    
    var body: some View {
        Text("Hello, world!")
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I get the following error:

A fetch request must have an entity.'
terminating with uncaught exception of type NSException

Am I missing something?

UPDATE: I removed the MyList.entity() part from the FetchRequest and now it works.

1 Answer 1

1

Remove

entity: MyList.entity(),

From the FetchRequest

Sign up to request clarification or add additional context in comments.

2 Comments

Yes. that works but why did it work when I was providing the correct entity.
@MaryDoe I think it is a bug, I’ve been getting it lately, I haven’t dug into why but I am assuming that there was an API update. Something changed on Apple’s end recently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.