核心数据总是删除第一个对象 [英] Core data always deletes first object

查看:47
本文介绍了核心数据总是删除第一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用coredata来存储FoodItems的应用程序中使用swiftUI.在我的应用程序中,我有一个存储在用户设备上的所有foodItems的列表.假设它们应该能够通过长按并按住吃或扔掉的方式删除任何foodItem,但是每次我尝试删除任何foodItem时,它总是删除列表中的第一个,而不是一个我长按了.谁能帮我?这是我的代码段:

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"))
            ])
        })

}

推荐答案

您应该改用 .actionSheet(item:,并将其移出 ForEach 循环,如下面的示例

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"))
    ])
})

这篇关于核心数据总是删除第一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆