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

Arrays Swift:删除文档字段Firestore(云数据库)内字典中的元素,arrays,swift,xcode,firebase,dictionary,Arrays,Swift,Xcode,Firebase,Dictionary,当前有一个UITableView,其中有一个编辑和删除按钮。现在,我正在尝试找出如何从数据库中删除地图/字典中的元素。例如,我想删除: 每日摄入量{ 1568695516{ 金额:12 时间戳:1568695516.837234 } 以下是我的数据库的图像: 这是我的Swift代码:{ @objc func handleDeleteTap() { print("Delete Button Tapped!") let deleteOption = UIAlertActi

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

每日摄入量{

1568695516{

金额:12

时间戳:1568695516.837234

}

以下是我的数据库的图像:

这是我的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)
}

感谢您的时间!请帮助!哈哈

尝试用事务更新字段

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


这里的文档应该有帮助:

您可以通过将元素传递给arrayRemove()来实现


所以我想出了如何在Firestore(云数据库)中删除DailyIntaction映射中的某个值

}


使用键旁边的“.”符号,它实际上会删除映射中的某个值。

我认为这篇文章可能对您的情况有所帮助。似乎不起作用,需要在ArrayMemove中输入一个值:“调用中缺少参数#1的参数”
FieldValue.arrayRemove(element)
 @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.1568695516" : 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)