这是我的Firebase数据结构
2016-10-24 Monday" : {
"types" : {
"LUNCH" : {
"typeA" : "2",
"typeB" : "1",
"typeC" : "1"
},
"DINNER" : {
"typeA" : "1",
"typeB" : "2",
"typeC" : "3"
}
}"2016-10-26 Wednesday " : {
"types" : {
"LUNCH" : {
"typeA" : "0",
"typeB" : "2",
"typeC" : "3"
},
"DINNER" : {
"typeA" : "2",
"typeB" : "2"}
"2016-10-27 Thursday " : {
"types" : {
"DINNER" : {
"typeA" : "2",
"typeB" : "2"
}
我现在要做的是:
如果在日期下有孩子("午餐"),我想知道他们是哪个日期 (在这种情况下是2016-10-24星期一和2016-10-26星期三)
我已经阅读了firebase库,但仍然不知道我应该使用什么方法
请在这里帮助我,非常感谢!!
答案 0 :(得分:1)
如果您拥有指定日期的快照,则可以通过致电FIRDataSnapshot.hasChild()
来检查儿童是否存在。
refHandle = dateRef.observe(FIRDataEventType.value, with: { (snapshot) in
if snapshot.hasChild("LUNCH") {
println("\(snapshot.key)")
}
})
答案 1 :(得分:1)
避免将数据结构嵌套得太深,将数据结构更改为: -
Dates:{
date1 : {
LUNCH : true,
DINNER : true
},
date2 : {
DINNER : true
}
},
LUNCH : {
date1 : {
"typeA" : "2",
"typeB" : "1",
"typeC" : "1"
},
date2 : {
"typeA" : "0",
"typeB" : "2",
"typeC" : "3"
}
},
DINNER :{
date1 : {
"typeA" : "1",
"typeB" : "2",
"typeC" : "3"}
}
获取键: -
FIRDatabase.database().reference().child("Dates").queryOrdered(byChild : "Lunch").queryEqual(toValue: true).observeSingleEvent(of: .value, with: {(snap) in
print(snap)
if let snapDict = snap.value as? [String:AnyObject]{
for each in snapDict{
print(each.key)
}
}
})