我是firebase的新手,我有我的firebase项目的结构
我希望得到所有对象,并且#34;感兴趣"价值等于"男人"
我写了这样的代码,让所有对象按interes值排序:
let thisUserRef = URL_BASE.childByAppendingPath("profile")
thisUserRef.queryOrderedByChild("Interest")
.observeEventType(.Value, withBlock: { snapshot in
if let UserInterest = snapshot.value!["Interest"] as? String {
print (snapshot.key)
}
}
但我收到零。
答案 0 :(得分:2)
您需要遍历所有键值个人资料
"players":{
"524050468":{
"name":"Disqualification",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"511471621":{
"name":"RedBaron_GR",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"519208358":{
"name":"pokefast",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"501168520":{
"name":"mad_2fast4u",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"517090729":{
"name":"Tcoglani",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"505935551":{
"name":"Ultimate_Spinach_",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"505118732":{
"name":"VADOR2",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"504449266":{
"name":"MirageIVS",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"507177678":{
"name":"wolrdofAlexis",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"501356239":{
"name":"drcop",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"516847314":{
"name":"MaXiMiLiAn_Gr",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"517675972":{
"name":"hell_fighter_gr",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"512160116":{
"name":"XmorrayX",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"507147831":{
"name":"xxx_Anti_xxx",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"500190361":{
"name":"laskas",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"503220794":{
"name":"Sniker71240",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"503172799":{
"name":"PuNniShZz",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"504438396":{
"name":"Oulamagos1980",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"GRBB",
"team":2,
"clanDBID":500063884,
"platoonID":19752025
},
"505065980":{
"name":"Fermierdu67",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
},
"501645375":{
"name":"marcus68510",
"prebattleID":19752025,
"igrType":0,
"clanAbbrev":"2DBFL",
"team":1,
"clanDBID":500029422,
"platoonID":19752025
}
}
此处_是格式 if let allProfiles = snapshot.value as? [String:AnyObject] {
for (_,profile) in allProfiles {
print(profile);
let userInterest = profile["Interest"]
}
}
的密钥,配置文件将是该密钥的元素。
修改强>:
根据docs查询子值。
尝试KYXA-random string
,然后使用我在答案中指定的内部循环
答案 1 :(得分:1)
This is a basic query in Firebase. (Updated for Swift 3, Firebase 4)
let profileRef = self.ref.child("profile")
profileRef.queryOrdered(byChild: "Interest").queryEqual(toValue: "men")
profileRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let dict = child as! [String: Any]
let name = dict["Name"] as! String
print(name)
}
})
The legacy documentation from Firebase really outlines how to work with queries: find it here
新文档相当薄。
哦,只是指出变量; thisUserNode应该是profileRef,因为它实际上是你查询的内容。