'Fetching data in Preview: "A fetch request must have an entity UncaughtExceptionError"

I have a ListView file which can build the preview code fine without the Core Data piece in it. However, when I build that ListView in the ContentView, I got an error that is not shown in the text editor like usual but in the diagnostics window. The error is "A fetch request must have an entity UncaughtExceptionError: [AppName] crashed due to an uncaught exception".

When I run the app in the simulator, the ContentView seems to build fine. (the functions attached to Text("+") don't work either, something about "map table argument is NULL", but one error at a time.)

EDIT: The entities in Core Data are set up as “Class Definition”.

Many many thanks in advance.

enter image description here

struct ListView: View {

@Environment(\.managedObjectContext) var viewContext
@FetchRequest(sortDescriptors: []) var targets: FetchedResults<TargetEntity>
@FetchRequest(sortDescriptors: []) var positives: FetchedResults<PositiveEntity>
   
var body: some View {
    
    VStack {
        HStack {
            VStack {
                    Text("+")
                        .onAppear{add()}
                        .onTapGesture (count: 2){
                            do {
                                print("error")
                                increment(targets.first!)
                                try viewContext.save()
                            } catch {
                                print("error")
                            }
                        }
                        .onLongPressGesture {
                            addPositive()
                        }
                
                List{
                    ForEach(positives) { item in
                        Text(item.title ?? "Unknown Title")
                    }
                    .onDelete(perform: deleteItems)
                }
            }
        }
    }
}

    func add() {
        let countnum = TargetEntity(context: viewContext)
        countnum.countnum = 0
        save()
    }

    func addPositive(){
        let newPositive = PositiveEntity(context: viewContext)
        newPositive.title = "Action"
        save()
    }

    private func deleteItems(offsets: IndexSet) {
         withAnimation {
             offsets.map { positives[$0] }.forEach(viewContext.delete)
             do {
                 try viewContext.save()
             } catch {
                 let nsError = error as NSError
                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
             }
         }
     }

    func save() {
        do { try viewContext.save() } catch { print(error) }
    }

}

struct ListView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView{
          ListView()//.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source