I have a simple list that displays array of some SwiftData models i.e.
List {
Section {
// .. Header
}
Section {
ForEach(myModels) {
ModelView(model: $0)
}
}
}
For simplicity sake, ModelView
is basically a Text
with .onTapGesture { delete() }
modifier, where delete is implemented like this
private func delete() {
DatabaseContainer.mainContext.delete(model)
try? DatabaseContainer.mainContext.save()
}
For a reason I don’t understand, my app crashes when I delete a model like this, with error trace looking like it’s trying to reference a deleted item somewhere, but it no longer exists.
If I change List to ScrollView
everything works fine (but obviously I loose benefits of the List).
Does List
require any special handling for cases like this?
EDIT: Adding .id(UUID())
to the list solves this, but it’s not an ideal solution as List “jump scrolls” to the top when deleting an item.