ios - 核心数据总是删除第一个对象

标签 ios swift core-data swiftui

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

    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循环,如下面的示例所示

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

关于ios - 核心数据总是删除第一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61976278/

相关文章:

swift - 用于将便捷构造函数添加到核心数据库中的通用函数

ios - 无法将类型 'Swift.Optional<Swift.AnyObject>' (0x7f9d44715db0) 的值转换为 'NSArray' (0x60000004f4f8)

iphone - 核心数据 - 查找具有重叠日期范围的记录

ios - 在 POST 请求中发送 JSON 对象 AFNetworking , iOS

javascript - 处理 Safari/Chrome 上的取消按钮以在 App Store 弹出窗口中打开

ios - 如何在iOS应用程序中将小数位限制为四位

ios - 我什么时候应该在 ios swift 上存储和重新存储到钥匙串(keychain)?

ios - 核心数据和数组

ios - 从 iOS5 通过引用和 objc_unretainedPointer 传递

iphone - 如何检测包含目标操作(对应于按钮单击)的 "viewcontroller"文件?