Deletion of a child in 1-many relationship causes SwiftData to crash
Here are the models
@Model
final class Plan: Codable {
var todos: [Todo]
}
@Model
final class Todo: Codable {
var summary: String
}
now the view is:
struct PlanView: View {
@Query private var plans: [Plan]
@Environment(.modelContext) private var modelContext
var body: some View {
NavigationStack {
ZStack {
if let currentPlan = plans.first {
List(currentPlan.todos) { todo in
Section {
Text("(todo.summary)") // <--- EXEC crash
}
}
}
}
}
}
and whenever I do a deletion like this it crashes:
struct TodoListView: View {
@Environment(.modelContext) private var modelContext
@Query private var todos: [Todo]
var body: some View {
...
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(todos[index])
}
}
}