我在安全和规则选项中有这个:
{
"rules": {
".read": true,
".write": true,
"groups": {
".indexOn": "name"
}
}
}
我的 JSON 结构是这样的:
{
"groups": {
"-KAti17inT7GbHEgbzzS": {
"author": "ruben",
"name": "C"
},
"-KAti36BQGO8sRmfufkE": {
"author": "ruben",
"name": "D"
},
"-KAti2CVAHtJllQm_m-W": {
"author": "ruben",
"name": "A"
}
}
如您所见,它并没有像预期的那样按“名称”排序。 我做错了什么?
最佳答案
安全规则中的 .indexOn
指令只告诉数据库在名称属性上创建索引。它不会自动重新排序数据。
此外:JSON 对象中键的顺序是未定义的。
要按名称获取群组列表,您必须执行 Firebase 查询。来自Firebase documentation on queries :
let ref = Firebase(url:"https://dinosaur-facts.firebaseio.com/dinosaurs")
ref.queryOrderedByChild("height").observeEventType(.ChildAdded, withBlock: { snapshot in
if let height = snapshot.value["height"] as? Double {
println("\(snapshot.key) was \(height) meters tall")
}
})
如果您根据您的数据结构调整此代码段,它将以正确的顺序打印组。
关于ios - Firebase queryOrderedByChild 不在 Swift 中按名称排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35506752/