【问题标题】:Core data always deletes first object核心数据总是删除第一个对象
【发布时间】:2020-05-23 17:51:16
【问题描述】:

我在我的应用程序中使用 swiftUI,它使用 coredata 来存储 FoodItems。在我的应用程序中,我有一个存储在用户设备上的所有 foodItems 的列表。假设他们应该能够通过长按吃或扔掉来删除任何一个食物项,但是每次我尝试删除任何食物项时,它总是删除列表中的第一个而不是一个我坚持了很久。谁能帮我?这是我的代码的 sn-p:

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

}

【问题讨论】:

    标签: ios swift core-data swiftui


    【解决方案1】:

    您应该改用.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"))
        ])
    })
    

    【讨论】:

    • 一旦我这样做,它就会显示错误,“实例方法 'actionSheet(item:content:)' 要求 'FoodItem' 符合 'Identifiable'”
    • @RyanD,FoodItem 只是我代码中的一个存根名称,因为您没有提供您的类型,但当然如果它需要使其符合 Identifiable,那么您应该符合它。要获得复制粘贴解决方案,您需要提供复制粘贴可测试代码,否则调整答案就在您身上。 ;)
    • 好的,谢谢,但如果它是来自核心数据的实体,我能确认它是可识别的吗? "@FetchRequest(entity: FoodItem.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \FoodItem.staysFreshFor, 升序: true)]) var foodItem: FetchedResults"
    • @RyanD,当然,如果您没有明确的字段来唯一标识食品项目,则可以使用 self 作为 id,因为它实际上是objective-c 引用类型对象。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2012-06-17
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多