user10081458
user10081458

Reputation:

Core data always deletes first object

I am using swiftUI in my app that uses coredata to store FoodItems. In my app I have a list of all the foodItems stored on the users device. Supposedly they are supposed to be able to delete any one of the foodItems by long holding and pressing either eat or throw away, but each time I try to delete any of the foodItems, it always deletes the first one in the list instead of the one I long holded. Can anyone help me? Here is a snippet of my code:

    ForEach(self.storageIndex.foodItemArray, id:\.self) { item in

    RefrigeratorItemCell(icon: item.wrappedSymbol, title: item.wrappedName, lastsUntil: self.addDays(days: Int(item.wrappedStaysFreshFor), dateCreated: item.wrappedInStorageSince))
        .gesture(LongPressGesture()
            .onEnded({ i in
                self.showEatActionSheet.toggle()
            })
    )
        //TODO: Make a diffrence between eat all and throw away
        .actionSheet(isPresented: self.$showEatActionSheet, content: {
            ActionSheet(title: Text("More Options"), message: Text("Chose what to do with this food item"), buttons: [
                .default(Text("Eat All"), action: {
                    self.managedObjectContext.delete(item)
                    try? self.managedObjectContext.save()
                })
                ,.default(Text("Throw Away"), action: {
                    self.managedObjectContext.delete(item)
                    try? self.managedObjectContext.save()
                })

                ,.default(Text("Cancel"))
            ])
        })

}

Upvotes: 1

Views: 74

Answers (1)

Asperi
Asperi

Reputation: 257533

You should use instead .actionSheet(item: and move it out of ForEach cycle, as on below scratchy example

@State private var foodItem: FoodItem? // << tapped item (use your type)

// .. other code

VStack {     // << some your parent container
    ForEach(self.storageIndex.foodItemArray, id:\.self) { item in

        RefrigeratorItemCell(icon: item.wrappedSymbol, title: item.wrappedName, lastsUntil: self.addDays(days: Int(item.wrappedStaysFreshFor), dateCreated: item.wrappedInStorageSince))
            .gesture(LongPressGesture()
                .onEnded({ i in
                    self.foodItem = item      // << tapped item
                })
        )
            //TODO: Make a diffrence between eat all and throw away
    }
} // actionSheet attach to parent
.actionSheet(item: self.$foodItem, content: { item in // << activated on item
    ActionSheet(title: Text("More Options"), message: Text("Chose what to do with this food item"), buttons: [
        .default(Text("Eat All"), action: {
            self.managedObjectContext.delete(item)
            try? self.managedObjectContext.save()
        })
        ,.default(Text("Throw Away"), action: {
            self.managedObjectContext.delete(item)
            try? self.managedObjectContext.save()
        })

        ,.default(Text("Cancel"))
    ])
})

Upvotes: 1

Related Questions