I have a fetchRequest and then I filter the FetchedResults Array. If I display the filtered Objects in a list and try to delete one of them and save, it crashes.
I get this error code: Thread 1: Simultaneous accesses to 0x7fe28344eb28, but modification requires exclusive access
I tried to filter the fetchRequest with an NSPredicate, but then it crashed as well.
struct ListView: View {
@FetchRequest(
entity: Item.entity(), sortDescriptors: []
) var itemList: FetchedResults<Item>
@Environment(\.managedObjectContext) var viewContext
var filteredItemList: Array<Item> {
return itemList.filter { $0.name == "TestItem1" }
}
var body: some View {
List {
ForEach(filteredItemList, id: \.self) { item in
Text(item.name)
}
.onDelete(perform: removeItem(at:))
}
}
private func removeItem(at offsets: IndexSet) { // Zum Entfernen von Sets durch Wischfunktion
for index in offsets {
let item = filteredItemList[index]
viewContext.delete(item)
}
try? self.viewContext.save()
}
}