I have problem with onDelete{}. when I tried to delete single row from list that contains data from coreData, it deleted the above row, not the row I want. ViewModel contains the notes array. for more details: when I put the first note and the second note, and then I deleted the first one, and put another one then delete the latest one coreData delete the second one not the third note I want to delete
@ObservedObject var delegate = ViewModel()
ForEach(delegate.notes) { note in
VStack(alignment:.leading){
Text(note.toDo!)
Text(note.datee!)
.font(Font.system(size: 9))
.foregroundColor(.gray)
}
}.onDelete(perform: removeNote)`
@FetchRequest(
entity: Note.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Note.toDo, ascending: true),
]
)
var notes : FetchedResults<Note>
func removeNote(offsets: IndexSet){
for index in offsets{
let note = notes[index]
// delegate.context.perform {
delegate.context.delete(note)
do{
try delegate.context.save()
delegate.refresh()
}catch{}
// }
}