Problem with SwiftData

When trying to delete the element from my list, I always got error in my model.

get {
        _$observationRegistrar.access(self, keyPath: \.id)
        return self.getValue(for: \.id) <-- ERROR: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1a949aefc)
    }

Because I am new in development, I don't know how to solve it.

Answered by twalters in 757636022

This may be related to the issue in the release notes.

After deleting an item, SwiftUI may attempt to reference the deleted content during the animation causing a crash. (109838173)

Workaround: Explicitly save after a delete.

Accepted Answer

This may be related to the issue in the release notes.

After deleting an item, SwiftUI may attempt to reference the deleted content during the animation causing a crash. (109838173)

Workaround: Explicitly save after a delete.

For folks wondering, I had to do this number:

.alert("Please confirm model deletion. Cannot be undone.", isPresented: $showDeletionConfirmation, actions: {
            Button("Confirm", role: .destructive) {
                record.deleteAllTheThings()
                dismiss()
                Task {
                    await MainActor.run {
                        context.delete(record)
                        try? context.save()
                    }
                    
                }
            }
            Button("Cancel", role: .cancel) { }
        })

And dismiss is in my view as:

@Environment(\.dismiss) var dismiss
Problem with SwiftData
 
 
Q