SwiftUI and lists with coreData, unexpected behavior, why it delete another row, not I selected

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{}
  //  }
}

Did you make sure it selects the correct index? Try printing the index before making the delete to see if its correct…

Also if you sorted or filtered the list then you should also filter your result first before deletion

yes! when I deleted a row it shifted all index and started from index 0 again and it printed the correct index

but the problem still there

Don’t save your context until after you have deleted all the items. When you save the context, it updates the list of items and screws up your indexing.

if I did not save the context, the deleted note will be loaded again(directly) to the list.

I found the solution when I tried to change fetching data into the array of notes by this line

var notes : FetchedResults<Note>