gpt4 book ai didi

swift - query.ref.removeValue 移除整个子节点而不仅仅是一个节点

转载 作者:行者123 更新时间:2023-11-28 08:31:16 24 4
gpt4 key购买 nike

我试图通过查询然后调用 query.ref.removeValue() 从我的数据库中删除一个特定节点,尽管每次运行这段代码时它都会删除整个子节点而不是仅仅删除节点。我的代码看起来像这样......

let query = (ref?.child("Groups").child("Martins").child("Messages").queryEqualToValue(selectedValue))

query!.ref.removeValue()

我的数据库看起来像这样......

It starts with the child named "Groups" then another which is the name of the group then a child named "Messages" and inside I want to be able to individuall delete the nodes instead of deleting the whole Messages child.

感谢所有的帮助,我真的很感激。

最佳答案

查询引用指向定义查询的位置,而不是它匹配的元素。所以 query!.ref.removeValue() 将删除整个位置。

要删除查询匹配的单个项目,您需要将监听器附加到查询并迭代子项:

let query = ref?.child("Groups").child("Martins").child("Messages")
.queryOrderedByKey("Item").queryEqualToValue(selectedValue)

query.observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
snapshot.ref.removeValue()
})

关于swift - query.ref.removeValue 移除整个子节点而不仅仅是一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38835857/

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