Delete Func Deletes All Items in List - SwiftUI

Viewed 54

When I select one of the items inside my list from my lists it only deletes the selected item.

But when I list all the lists and their reminders inside the AllView it deletes all of the reminders inside the list.

How can I overcome that problem?

To tell my problem clearly I have two videos that show both cases.

First Case

Second case

it is my delete button inside ReminderCell view

struct ReminderCell: View {
@Environment(\.managedObjectContext) private var viewContext
var reminder: CDReminder
@State var isSelected: Bool

Button(action: {
        self.isSelected = true
        DispatchQueue.main.asyncAfter(deadline: .now() + 1){
            deleteReminder(at: Int(reminder.index))
        }

and again inside the ReminderCell I have deleteReminder func

func deleteReminder(at offsets: Int) {
    viewContext.delete(reminder)
    PersistenceController.shared.saveContext()
}

Inside the AllView I am calling listDetailCell as

struct AllView: View {
    @State var title = ""
    @State var note = ""
    @State var releaseDate = Date()
    @ObservedObject var list : CDListModel
    @State var selectedList = CDListModel()
    @Environment(\.presentationMode) var mode
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest var lists: FetchedResults<CDListModel>
    init(){
        list = CDListModel()
        let request: NSFetchRequest<CDListModel> = CDListModel.fetchRequest()
        request.sortDescriptors = [NSSortDescriptor(keyPath: \CDListModel.name, ascending: true)]
        request.entity = CDListModel.entity()
        _lists = FetchRequest(fetchRequest: request)
    }


var body: some View {
                List{
                    ForEach(lists, id: \.self) { list in
                        ListDetailCell(list: list)
                       }
                      }
                     }

My ListDetailCell

    struct ListDetailCell: View {
    @State var title = ""
    @ObservedObject var list : CDListModel
    @State var selectedList: CDListModel!
    @State var isAddReminderTapped = false
    @Environment(\.managedObjectContext) private var viewContext
    var body: some View {
        VStack(alignment: .leading){
            Text(list.text ?? "")
            ForEach((list.reminders?.allObjects as! [CDReminder]).indices , id: \.self) { reminderIndex in
                ReminderCell(reminder: (list.reminders?.allObjects[reminderIndex]) as! CDReminder, isSelected: false, selectedList: $selectedList, onComplete: {})
            }
        }
    }
}
2 Answers

Your delete function is wrong.

Here you are passing an offsets: Int. But you are never using that offsets inside the function. You are just deleting the whole reminder.

func deleteReminder(at offsets: Int) {
    viewContext.delete(reminder)
    PersistenceController.shared.saveContext()
}

Somehow using ForEach inside the List was causing this problem in AllView.

When I change the body of the AllView like below my problem disappeared.

NavigationView {
        ScrollView {
            VStack{
                HStack{
                    Text("Tumu")
                        .font(.system(size: 40, weight: .bold, design: .rounded))
                        .foregroundColor(.gray)
                    Spacer()
                }
                .padding(.leading)
                    ForEach(lists, id: \.self) { list in
                        ListDetailCell(list: list)
                    }
Related