从带有部分的 SwiftUI 列表中删除 CoreData

Deleting CoreData from SwiftUI List with Sections

目标

我想从列表中的 ForEach 上的 SectionedFetchRequest 中删除一个项目。我找到的唯一解决方案是针对常规 FetchRequest 我已经设法将其从 UIList 中删除,但没有从 CoreData 的 ViewContext.

中删除

我的问题很独特,因为我试图从不同于 FetchRequest

的 SectionedFetchRequest 中删除
    @SectionedFetchRequest(entity: Todo.entity(), sectionIdentifier: \.dueDateRelative, sortDescriptors: [NSSortDescriptor(keyPath: \Todo.dueDate, ascending: true)], predicate: nil, animation: Animation.linear)
    var sections: SectionedFetchResults<String, Todo>
    var body: some View {
        NavigationView {
            List {      
                ForEach(sections) { section in
                    Section(header: Text(section.id.description)) {
                        ForEach(section) { todo in
                            TodoRowView(todo: todo)
                                .frame(maxWidth: .infinity)
                                .listRowSeparator(.hidden)
                        }
                        .onDelete { row in
                            deleteTodo(section: section.id.description, row: row)
                            }
                        }

                    }
                }
    func deleteTodo(section: String, row: IndexSet) {
        // Need to delete from list and CoreData viewContex.
    }
// My old way of deleting notes with a regular fetch Request
func deleteNote(at offsets: IndexSet) {
    for index in offsets {
        let todo = todos[index]
        viewContext.delete(todo)
    }
    try? viewContext.save()
}

这就是您使用 link...

的方式

将此添加到 TodoRowView(todo: todo)

.swipeActions(content: {
    Button(role: .destructive, action: {
        deleteTodo(todo: todo)
    }, label: {
        Image(systemName: "trash")
    })
})

并且您在 View

中需要此方法
public func deleteTodo(todo: Todo){
    viewContext.delete(todo)
    do{
        try viewContext.save()
    } catch{
        print(error)
    }
}

或者您可以使用在 ForEach

上使用 onDelete 的当前设置
.onDelete { indexSet in
    deleteTodo(section: Array(section), offsets: indexSet)
    }

就是用这个方法

func deleteTodo(section: [Todo], offsets: IndexSet) {
    for index in offsets {
        let todo = section[index]
        viewContext.delete(todo)
    }
    try? viewContext.save()
}

当然,要使其中任何一项发挥作用,您都需要一个有效的

@Environment(\.managedObjectContext) var viewContext

在文件的顶部