I am building a small app using SwiftUI and CoreData with the SwiftUI App life cycle(no scene or app delegate). I'm getting the following error when I Run+Build my app:
'executeFetchRequest:error: A fetch request must have an entity.'
I've checked/verified/re-checked the following:
- My
[app name].xcdatamodeldfile name is the same as what I pass into the NSPersistentContainerNSPersistentCloudKitContainer(name: [app name]) - The entity's name
Caris exactly what I pass into the FetchRequest
@FetchRequest(entity: Car.entity(), sortDescriptors: []) var car: FetchedResults<Car>
- I am selecting Manual/None for my entities Codegen and the generated class is
public class Car: NSManagedObject {}
with an extension on Car of Identifiable.
Here is my whole view struct that should be(to my understanding) passing the environment around to all of its "child" views.
struct AppView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Car.entity(), sortDescriptors: []) var car: FetchedResults<Car>
var body: some View {
List {
ForEach(car, id:\.self) { item in
RootView(carData: item)
.environment(\.managedObjectContext, self.moc)
}
}
}
}
and my @main struct
@main
struct AutoMateApp: App {
@StateObject var coreData = PersistentCloudKitContainer()
let persistence = PersistentCloudKitContainer()
var body: some Scene {
WindowGroup {
AppView()
.environment(\.managedObjectContext, coreData.persistentContainer.viewContext)
}
}
}
When I step through with the debugger the crash seems to appear once the WindowGroup is returned. I'm not sure if that's helpful information at all.
I appreciate all your help, thank you.
A fetch request must have an entity.