我按照this教程制作了一个带有firebase同步和身份验证的应用。但现在,我不知道如何在所有孩子中进行搜索,因为这些孩子都有基于词典编纂的密钥。
更具体地说,从这个例子中,你如何从所有“用户”查询所有“currentCity”
{
"users" : {
"1e2f048f-a3a0-4190-afad-81d724ed1997" : {
"currentCity" : "Arrecifes",
"currentCountry" : "ARG",
"currentState" : "BSA",
"day" : {
"domingo" : true,
"jueves" : true,
"martes" : false,
"miercoles" : true
},
"gender" : "Masculino",
"name" : "Guillermo H Acosta",
"position" : "Medio",
"registered" : true,
"timeSince" : "22:00",
"timeUntil" : "23:00",
"whereToPlay" : "Güemes"
},
"39c6ccf9-61ec-446e-9af3-3a87810fab71" : {
"currentCity" : "Alvear",
"currentCountry" : "ARG",
"currentState" : "CRR",
"day" : {
"jueves" : true,
"miercoles" : true,
"viernes" : true
},
"gender" : "Masculino",
"name" : "Guillermo Acosta",
"registered" : true,
"timeSince" : "21:00",
"timeUntil" : "23:00"
},
"4991bdc9-dfc4-4ff5-ab81-0e28f1b3ab53" : {
"currentCity" : "Cordoba",
"currentCountry" : "ARG",
"currentState" : "COR",
"day" : {
"miercoles" : true,
"viernes" : true
},
"gender" : "Masculino",
"name" : "Hsjja",
"timeSince" : "02:00",
"timeUntil" : "03:00"
},
"509364fd-388b-42ff-91ed-0811811a4ff3" : {
"day" : {
"jueves" : true,
"lunes" : true,
"martes" : false,
"sabado" : true
},
"name" : "Guillermo Acosta",
"position" : "Delantero",
"timeSince" : "13:00",
"timeUntil" : "14:00",
"whereToPlay" : "Güemes"
}
}
}
答案 0 :(得分:1)
使用Firebase数据库,您可以获得节点,也可以不获取该节点。无法在查询中获取每个节点的一部分。
因此,如果您只想加载所有用户的城市,则应保留一个仅包含该信息的节点:
{
"userCurrentCities" : {
"1e2f048f-a3a0-4190-afad-81d724ed1997" : "Arrecifes",
"39c6ccf9-61ec-446e-9af3-3a87810fab71" : "Alvear",
"4991bdc9-dfc4-4ff5-ab81-0e28f1b3ab53" : "Cordoba",
}
}
答案 1 :(得分:0)
这将解析每个节点抓取currentCity键。如果currentCity为NULLABLE
,我建议将其存放在单独的节点中。
let ref = // path to users here
ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
// Put all of the users into a dictionary
let postDict = snapshot.value as! [String : AnyObject]
for user in postDict {
// Set each child of the user as a dictionary
let data = activity.1 as! [String : AnyObject]
// Get the specific child
let currentCity = children["currentCity"] as! String
// Do something with the data
print(currentCity)
}
})