删除 SwiftUI 列表中的项目(由核心数据支持)后出现 "Thread 1: EXC_BAD_INSTRUCTION" 错误? (附代码)
after delete of item in SwiftUI List (backed by Core Data) getting a "Thread 1: EXC_BAD_INSTRUCTION" error? (code attached)
完成 SwiftUI 列表中一行的删除后,出现 "Thread 1: EXC_BAD_INSTRUCTION" 错误。似乎核心数据删除在我重新启动数据已被删除后起作用。因此,在执行核心数据删除后,可能与 SwiftUI 尝试更新其视图有关????
代码:
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) var context
@FetchRequest(entity: GCItem.entity(), sortDescriptors: []) var gcItems: FetchedResults<GCItem>
private func addItem(title:String) {
let newItem = GCItem(context: context)
newItem.id = UUID()
newItem.title = title
do {
try context.save()
} catch let e as NSError {
fatalError("Unresolved error \(e), \(e.userInfo)")
}
}
private func deleteItem(at offsets:IndexSet) {
self.context.perform {
// Delete Item
for index in offsets {
let item = self.gcItems[index]
self.context.delete(item)
}
// Persist
do {
try self.context.save()
} catch let e as NSError {
// TODO: How to undelete list???
print("ERROR : Can not save GCItem items: \(e.description)")
}
}
}
var body: some View {
NavigationView {
VStack {
List() {
ForEach(gcItems) { gcItem in
HStack {
Text("test")
}
}
.onDelete(perform: self.deleteItem)
}
Button(action: { self.addItem(title: "Testing 123") }) {
Text("ADD ITEM")
}
}
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return ContentView().environment(\.managedObjectContext, context)
}
}
#endif
屏幕截图
解决方法是不使用名为 "id" 的核心数据中的属性。所以我将其更改为 "myId" 然后一切正常。
完成 SwiftUI 列表中一行的删除后,出现 "Thread 1: EXC_BAD_INSTRUCTION" 错误。似乎核心数据删除在我重新启动数据已被删除后起作用。因此,在执行核心数据删除后,可能与 SwiftUI 尝试更新其视图有关????
代码:
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) var context
@FetchRequest(entity: GCItem.entity(), sortDescriptors: []) var gcItems: FetchedResults<GCItem>
private func addItem(title:String) {
let newItem = GCItem(context: context)
newItem.id = UUID()
newItem.title = title
do {
try context.save()
} catch let e as NSError {
fatalError("Unresolved error \(e), \(e.userInfo)")
}
}
private func deleteItem(at offsets:IndexSet) {
self.context.perform {
// Delete Item
for index in offsets {
let item = self.gcItems[index]
self.context.delete(item)
}
// Persist
do {
try self.context.save()
} catch let e as NSError {
// TODO: How to undelete list???
print("ERROR : Can not save GCItem items: \(e.description)")
}
}
}
var body: some View {
NavigationView {
VStack {
List() {
ForEach(gcItems) { gcItem in
HStack {
Text("test")
}
}
.onDelete(perform: self.deleteItem)
}
Button(action: { self.addItem(title: "Testing 123") }) {
Text("ADD ITEM")
}
}
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return ContentView().environment(\.managedObjectContext, context)
}
}
#endif
屏幕截图
解决方法是不使用名为 "id" 的核心数据中的属性。所以我将其更改为 "myId" 然后一切正常。