Duplicate rows in List populated by CoreData

This is an interesting issue I am having with a custom app I am making in Xcode using SwiftUI.

I have a Swift file called BillView. In this file, I have some code that displays data retrieved from the CoreData Stack in a List. The issue I am having is when I add a new record, the List reflects that there is an additional row but it has the same displayed text as the one above it. If I continue to add records, they all show the same text as the first row. I have gone through the code and banged my head against the wall until it now hurts. Can someone please help me discover why each row isn't showing its unique data? I have attached the code from the BillView Swift file below.

//

//  BillView.swift

//  Budget Me

//

//  Created by Charles Vincent on 8/3/22.

//



import SwiftUI

import CoreData



struct BillRow: View {

    var bill: Bill

    var body: some View {

        Text(bill.merchant ?? "No merchant given")

    }

}



struct BillView: View {

    @Environment(\.managedObjectContext) private var viewContext

    @FetchRequest(

        entity: Bill.entity(),

        sortDescriptors: [NSSortDescriptor(keyPath: \Bill.date, ascending: true)],

        animation: .default)

    private var bills: FetchedResults<Bill>

    @State var linkActive = false

    var body: some View {

        NavigationView {

            List {

                ForEach(bills){ bill in

                    BillRow(bill: bill)

                }

                .onDelete(perform: deleteBills)

            }

            .navigationTitle("Bills")

            .background(

                NavigationLink(destination: AddBillView(), isActive: $linkActive) {}

            )

            .toolbar {

                ToolbarItem(placement: .automatic) {

                    Button(action: {

                        linkActive = true

                    }) {

                        Image(systemName: "plus")

                    }

                }

            }

        }

    }

    private func deleteBills(offsets: IndexSet) {

        withAnimation {

            offsets.map { bills[$0] }.forEach(viewContext.delete)

            

            do {

                try viewContext.save()

            } catch {

                // Replace this implementation with code to handle the error appropriately.

                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                let nsError = error as NSError

                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")

            }

        }

    }

}







struct BillView_Previews: PreviewProvider {

    static var previews: some View {

        BillView()

    }

}

Show the code where you add the bill to Core Data and the code for AddBillView. The most likely source of your problem is in that code.

I fixed the issue!

Turns out, in this bit of code ForEach(bills){

I needed to have this instead ForEach(bills, id: \.self){

That fixed it!

I am also facing same problem in my app each time I called API and save data into database @FetchRequest append new record even I am clearing table each time but it duplicates the data

Duplicate rows in List populated by CoreData
 
 
Q