gpt4 book ai didi

swift - 使用 Firebase 和 Swift 3 的一对多查询

转载 作者:可可西里 更新时间:2023-11-01 00:58:20 25 4
gpt4 key购买 nike

我有一个应用程序,它有主题和类别,一个类别可能属于一个或多个主题,我正在尝试过滤数据以仅显示具有标记为 true 的特定主题 ID 的类别,这是结构: enter image description here Categories with references to the topics ID.

这是我想出的代码(在 Swift 2.3 上运行):

self.ref = FIRDatabase.database().reference(fromURL: FIREBASE_URL).child("categories")
let query = ref?.queryOrdered(byChild: "topics/idt2").queryEqual(toValue: true)
query!.observe(.value, with: { (snapshot) in
//This should bring back both categories, Soccer and Moon

print("Inside query \(snapshot.value)") // Prints null

})

有什么想法吗?

最佳答案

当您对 Firebase 数据库执行查询时,可能会有多个结果。所以快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。

您需要在回调 block 中处理此列表:

query!.observe(.value, with: { (snapshot) in
for child in snapshot.children {
print(child.key)
}
})

另见:

关于swift - 使用 Firebase 和 Swift 3 的一对多查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39810255/

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