gpt4 book ai didi

ios - Firebase - .value 上的索引未给出排序结果

转载 作者:行者123 更新时间:2023-11-28 19:30:27 25 4
gpt4 key购买 nike

我有这样的数据结构:

{
"inbox_events": {
"from_users": {
"uid1": {
"uid2": 1501337181,
"uid3": 1501337183,
"uid4": 1501337179
},
"uid2" : {
"uid6": 1501337284,
"uid1": 1501337295
}
}
}
}

遵循以下规则:

{
"rules": {
".read": "auth != null",
".write": "auth != null",
"inbox_events": {
"from_users": {
"$userId": {
".indexOn": ".value"
}
}
}
}
}

问题:我不明白为什么值(value)索引不起作用。在 firebase 控制台中,每个 $userId 的条目仍然按字母顺序排列,如果我执行 queryOrderedByValue,我不会得到排序结果。

我是否误读了 Firebase 文档?

提前致谢!


编辑:

具体来说,我正在尝试像这样查询数据:

let queryRef = Database.database().reference().child("inbox_events").child("from_users").child("uid1").queryOrderedByValue()
queryRef.observe(.value, with: { (snapshot) in
print("key: \(snapshot.key), value: \(snapshot.value)")
})

为了获取 uid1 节点处的 uid 的排序列表,按降序值排序。

最佳答案

这很容易被忽视。

结果被合并到一个快照中,打印时,数据被转换成一个失去顺序的字典。

为了保持正确的顺序,迭代返回的快照。

这是你的代码和转换后的快照,然后是如何迭代快照以保持顺序

let queryRef = self.ref.child("inbox_events").child("from_users").child("uid1").queryOrderedByValue()
queryRef.observe(.value, with: { (snapshot) in

//converts to dict
print("key: \(snapshot.key), value: \(snapshot.value)")

//keeps the order
for child in snapshot.children {
let snap = child as! DataSnapshot
print("key: \(snap.key), value: \(snap.value)")
}
})

关于ios - Firebase - .value 上的索引未给出排序结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45398889/

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