gpt4 book ai didi

arrays - Swift:删除文档字段Firestore(云数据库)内字典中的元素

转载 作者:行者123 更新时间:2023-11-30 10:36:13 25 4
gpt4 key购买 nike

当前有一个 UITableView,其中有一个编辑和删除按钮。现在我试图弄清楚如何从数据库中删除 map /字典中的元素。例如,我想删除:

每日摄入量{ .

1568695516 {

数量:12。

时间戳:1568695516.837234

}

这是我的数据库的图像: Firestore Image .

这是我的 Swift 代码:{

    @objc func handleDeleteTap() {
print("Delete Button Tapped!")

let deleteOption = UIAlertAction(title: "Delete", style: .destructive) { (action) in
do {
// handle delete logic in the backend and update tableview
collectionReference(to: .intake).document(userUID).updateData([
"dailyIntake" : FieldValue.arrayRemove()
])

} catch let err {
print("Failed to Sign Out with Error:", err)
CustomAlert.showAlert(on: self, style: .alert, title: "Deletion Error", message: err.localizedDescription)
}
}

let cancelOption = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
CustomAlert.showAlert(on: self, style: .alert, title: "Delete current log?", message: nil, actions: [deleteOption, cancelOption], completion: nil)
}

非常感谢您的宝贵时间!请帮忙!哈哈

最佳答案

尝试使用事务更新字段

首先读取字典字段,删除字典中的元素,然后更新字段。

这里的文档应该有帮助:https://firebase.google.com/docs/firestore/manage-data/transactions

关于arrays - Swift:删除文档字段Firestore(云数据库)内字典中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com