onDelete not working properly in my app

Hello

In my app I have a list of items in a ForEach and at the end of the ForEach there is a .onDelete. (This is in a View that I called AllHistoryView)

The items are represented by a CoreData NSManagedObject that I called LifetimeInputs with 3 properties:

date: Date, imageTemplate: String, valori: Double

The problem is that, when I add (in a different View) an item with the date of tomorrow and then I add another to item with the date of today and then go to the AllHistoryView where there are all the items and delete slowly the item with date of tomorrow (especially at the end of the swipe action) instead of deleting the item that I swiped it deletes the on before it, how can I solve this problem?

Here is the AllHistoryView code:

import SwiftUI
import CoreData

struct AllHistoryView: View {
    
    @Environment(\.managedObjectContext) var viewContext
    
    @FetchRequest(entity: LifetimeInputs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], animation: .default) var lifetimeInputsModel: FetchedResults<LifetimeInputs>
    
    @State private var text: String = ""
    @State private var ascending: Bool = true
    
    @State var sortDescriptor: NSSortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)
    
    @Environment(\.dynamicTypeSize) var dynamicTypeSize
    
    var size: CGFloat{
        if UIDevice.current.userInterfaceIdiom == .phone {
            switch dynamicTypeSize {
            case .xSmall: return 11
            case .small: return 13
            case .medium: return 15
            case .large: return 17 
            case .xLarge: return 19
            case .xxLarge: return 21
            case .xxxLarge: return 23
            default: return 23
            }
        } else {
            switch dynamicTypeSize {
            case .xSmall: return 13
            case .small: return 15
            case .medium: return 17
            case .large: return 19 
            case .xLarge: return 21
            case .xxLarge: return 23
            case .xxxLarge: return 25
            case .accessibility1: return 27
            case .accessibility2: return 29
            default: return 29
            }
        }
    }
    
    var body: some View {
        theView()
    }
    
    @ViewBuilder
    func theView() -> some View{
        NavigationView{
            if !lifetimeInputsModel.isEmpty{
                List{
                    SectionList(sortDescripter: sortDescriptor, text: $text)
                        .environment(\.managedObjectContext, viewContext)
                        .onChange(of: ascending, perform: { _ in
                            if ascending {
                                withAnimation {
                                    sortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)
                                }
                            } else {
                                withAnimation {
                                    sortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: false)
                                }
                            }
                        })
                }
                .searchable(text: $text, placement: .navigationBarDrawer, prompt: "Quantity or date".localized())
                .navigationBarTitle("History", displayMode: .inline)
                .toolbar{
                    ToolbarItem(placement: .automatic) {
                        Image(systemName: "arrow.up.arrow.down.circle")
                            .foregroundColor(.primary)
                            .font(.system(size: size))
                            .rotation3DEffect(.degrees(ascending ? 0 : 180), axis: (x: 1, y: 0, z: 0))
                            .opacity(0.5)
                            .onTapGesture {
                                ascending.toggle()
                            }
                    }
                }
            } else{
                VStack{
                    Text("No Data".localized())
                        .font(.largeTitle)
                        .fontWeight(.semibold)
                        .foregroundColor(.secondary)
                }
                .padding(.bottom)
                .navigationBarTitle("History", displayMode: .inline)
            }
        }
    }
}

struct SectionList: View {
    
    @Environment(\.managedObjectContext) var viewContext
    
    @FetchRequest var lifetimeInputsModel: FetchedResults<LifetimeInputs>
    
    @Binding var text: String
    
    init(sortDescripter: NSSortDescriptor, text: Binding<String>) {
        let request: NSFetchRequest<LifetimeInputs> = LifetimeInputs.fetchRequest()
        request.sortDescriptors = [sortDescripter]
        _lifetimeInputsModel = FetchRequest<LifetimeInputs>(fetchRequest: request)
        
        self._text = text
    }
    
    @FetchRequest(entity: Limit.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Limit.date, ascending: true)], animation: .default) var limit: FetchedResults<Limit>
    
    @Environment(\.dynamicTypeSize) var dynamicTypeSize
    
    var size: CGFloat{
        if UIDevice.current.userInterfaceIdiom == .phone {
            switch dynamicTypeSize {
            case .xSmall: return 11
            case .small: return 13
            case .medium: return 15
            case .large: return 17
            case .xLarge: return 19
            case .xxLarge: return 21
            case .xxxLarge: return 23
            default: return 23
            }
        } else {
            switch dynamicTypeSize {
            case .xSmall: return 13
            case .small: return 15
            case .medium: return 17
            case .large: return 19 
            case .xLarge: return 21
            case .xxLarge: return 23
            case .xxxLarge: return 25
            case .accessibility1: return 27
            case .accessibility2: return 29
            default: return 29
            }
        }
    }
    
    @StateObject var lifeTimeInputsViewModel = LifeTimeInputsViewModel()
    
    var body: some View {
        Section{
            ForEach(lifetimeInputsModel.filter { text.isEmpty || "\($0)".contains(text) }){ lifetimeInputs in
                HStack{
                    Text("\(lifetimeInputs.valori, specifier: format(unita: !limit.isEmpty ? limit[0].unita ?? ml : ml)) \(!limit.isEmpty ? limit[0].unita ?? ml: ml)")
                        .font(.system(size: size))
                    
                    Spacer()
                    
                    Text("\(dateFormatter.string(from: lifetimeInputs.date ?? Date()))")
                        .font(.system(size: size))
                }
            }
            .onDelete(perform: { offsets in
                lifeTimeInputsViewModel.deleteItems(offsets: offsets)
            })
        } header: {
            Text("History".localized()).font(.system(size: size - 4))
        }
    }
}

Thank You very much!

onDelete not working properly in my app
 
 
Q