我对queryOrdered()
函数有问题,我真的不知道该如何处理...
假设我的数据库结构是:
Planes{
Uid{
SE{
AutoID{
.Auto-Id
.Picture
.Registration
.Type
.model
}
AutoID{
//Same as first one
}
}
ME{
AutoID{
//Same as first one
}
}
}}
在数据库的这一部分,我想通过.Registration
进行搜索
因此,我已将参考设置为Planes > UId
然后执行以下代码:
var acftPickerSelected = aircrafts[listAcftsPicker.selectedRow(inComponent: 0)]
if let user = Auth.auth().currentUser{
// user is connect
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
let ev = ref.child("Planes").child(userID!)
ev.queryOrdered(byChild: "Registration").queryEqual(toValue: acftPickerSelected).observeSingleEvent(of: .value) { (snapshot: DataSnapshot) in
for child in snapshot.children {
let value = snapshot.value as? NSDictionary
self.pickerRegi = value?["Registration"] as? String ?? "..unknown.."
self.stdbPickerType = value?["Type"] as? String ?? "..unknown.."
self.pickerModel = value?["model"] as? String ?? "..unknown.."
print("Avion : \(self.pickerRegi) - \(self.pickerModel) - \(self.stdbPickerType)")
}
}
但是出现此消息:
2018-11-27 18:03:54.797824 + 0100 XXXXXXXXXX [12954:527986] 5.10.0-[Firebase / Database] [I-RDB034028]使用未指定的索引。您的数据将在客户端上下载并过滤。考虑将/ Planes / BJMhmQzJXldULc5X9Y2WzjFEs9a2上的“ .indexOn”:“注册”添加到安全规则中,以提高性能
所以我一直在Firebase上添加如下规则:
{"rules": {
"Planes":{
"$uid":{
".indexOn": ["Registration"],
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
},
".read": true,
".write": true}}
现在错误消息不再出现,但是在此部分中什么也没有发生:
self.pickerRegi = value?["Registration"] as? String ?? "..unknown.."
self.stdbPickerType = value?["Type"] as? String ?? "..unknown.."
self.pickerModel = value?["model"] as? String ?? "..unknown.."
print("Avion : \(self.pickerRegi) - \(self.pickerModel) - \(self.stdbPickerType)")
我该如何解决?
感谢您的帮助。
答案 0 :(得分:0)
这里发生了两件事;
,在这种情况下,indexOn不是问题。
indexOn在生产应用程序中最重要,基本上可以提高查询性能。对于测试,请首先消除该变量,因为它不是问题的根源。将规则暂时重置为默认值
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
真正的问题是观察,因为它的深度不足以获取您要获取的数据。您缺少“ SE”级别。这是一个重写
let uidRef = ref.child("Planes").child(userID)
let seRef = uidRef.child("SE") //this is the child node to query
seRef.queryOrdered(byChild: "Registration").queryEqual(toValue: acftPickerSelected)
.observeSingleEvent(of: .value) { snapshot in
for child in snapshot.children {
let childSnap = child as! DataSnapshot
let dict = childSnap.value as! [String: Any]
let reg = dict["Registration"] as? String ?? "No Reg"
let mod = dict["model"] as? String ?? "No Model"
print(reg, mod)
}
}
问题的编写方式,该查询将查询下一个级别:它期望直接在SE和ME节点下找到一个Registration节点。但是这些节点仅包含autoId节点。
编辑:
通过展平(去规范化)更改结构也是另一种选择
Planes
Uid
AutoID
.Auto-Id //note, if the is the same as the key, its not needed
.Picture
.Registration
.Type
.model
.se_or_me: "SE"
AutoID
.Auto-Id
.Picture
.Registration
.Type
.model
.se_or_me: "ME"