如何在一个查询Firebase中检索多个数据 [英] How to retrieve multiple data in one query Firebase
问题描述
$ p $
{Guardians:{
jojo-pti-gros:{
-KBT2ui6wlC6Fgk1c7Xa:{
creationDate:2016-02-26 15:50:56,
level:2 ,
light:2,
type:Titan
}
}
},
Players:{
jojo-pti-gros:{
console:PS4,
creationDate:2016-02-26 15:50:39,
currentGuardian:-KBT2ui6wlC6Fgk1c7Xa,
email:toto@gmail.com,
gamertag:jojo-pti-gros,
guardians:{
-KBT2ui6wlC6Fgk1c7Xa:true
}
}
},
团队:{
-KBhfH9U3CxQPZiCs5ru:{
活动:Raid - Normal - Crota结束,
creationDate:2016-02-29 16:37:14,
language :任何语言,
leader:jojo-pti-gros,
maxPlayers:6,
mic:true,
minimumLight:55,
players:{
jojo-pti-gros:true
},
teamDescription:Chaud pour pour un raid
}}
当我尝试检索一个团队时,
$ b
- KBhfH9U3CxQPZiCs5ru= {
activity =Raid - Normal - Crota's end,
creationDate =2016-02-29 16:37:14,
language =任何语言,
leader =jojo-pti-gros,
maxPlayers = 6,
mic = true,
minimumLight = 55,
players = {
jojo-pti-gros= {
creationDate= 2016-02-26 15:50:56,
level=2,
light=2,
type=Titan
}
},
teamDescription =Chau d倒un raid
}
但是我有这个json而不是
- KBhfH9U3CxQPZiCs5ru= {
activity =Raid - Normal - Crota's end,
creationDate =2016-02 -29 16:37:14,
language =Any language,
leader =jojo-pti-gros,
maxPlayers = 6,
mic = true,
minimumLight = 55,
players = {
jojo-pti-gros= true
},
teamDescription =Chaud pour un raid
}
我的代码现在
let fb = Firebase(url:myUrl)
fb.childByAppendingPath(/ Teams)。observeSingleEventOfType(.Value,withBlock:{teams in
var teamsArray:[Team ](=
){
print(teams.value)
(如果(teams.value是NSNull)为teams.value团队! NSDictionary {
let newTeam = Team()
newTeam.dictionaryToTeam(team.value as!NSDictionary)
newTeam.id = String(team.key)
用于newTeam中的玩家。 playerDictionary as NSDictionary {
GuardianAPI.sharedInstance.getGuardianByGamertagAndID(player.key as!String,id:player.value as!String,handler:{newGuardian in
newTeam.playerDictionary.setValue(newGuardian,forKey:player .key as!String)
})
}
teamsArray.append(newTeam)
}
handler(true)
}
} )
我不知道如何使用SQL等关键字进行查询。
有人可以知道如何解决这个问题吗?
p>
一种选择是读取你的团队节点,将玩家参考分成一个数组,然后迭代数组以获得每个玩家的参考,并为每个玩家分别加载一个单独的Firebase调用
一个更好的选择是在每个玩家的节点中存储一个引用给团队
Players:{
jojo-pti-gros:{
team:-KBhfH9U3CxQPZiCs5ru
}
观察球队节点加载球队数据,然后查询球员节点中所有有小孩的球员:team =-KBhfH9U3CxQPZiCs5ru
Firebase * playersRef = [myRootRef childByAppendingPath:@Players];
FQuery * query1 = [playersRef queryOrderedByChild:@team];
FQuery * query2 = [query1 queryEqualToValue:@ - KBhfH9U3CxQPZiCs5ru];
[query2 observeEventType:FEventTypeValue withBlock:^(FDataSnapshot * snapshot){
NSLog(@key:%@%@,snapshot.key,snapshot.value) ;
}];
结果将会是所有属于队伍的玩家的快照。
I'm new in firebase and nosql. I followed the guide for structuring data and here is what my data look like
{"Guardians" : {
"jojo-pti-gros" : {
"-KBT2ui6wlC6Fgk1c7Xa" : {
"creationDate" : "2016-02-26 15:50:56",
"level" : "2",
"light" : "2",
"type" : "Titan"
}
}
},
"Players" : {
"jojo-pti-gros" : {
"console" : "PS4",
"creationDate" : "2016-02-26 15:50:39",
"currentGuardian" : "-KBT2ui6wlC6Fgk1c7Xa",
"email" : "toto@gmail.com",
"gamertag" : "jojo-pti-gros",
"guardians" : {
"-KBT2ui6wlC6Fgk1c7Xa" : "true"
}
}
},
"Teams" : {
"-KBhfH9U3CxQPZiCs5ru" : {
"activity" : "Raid - Normal - Crota's end",
"creationDate" : "2016-02-29 16:37:14",
"language" : "Any language",
"leader" : "jojo-pti-gros",
"maxPlayers" : "6",
"mic" : "true",
"minimumLight" : "55",
"players" : {
"jojo-pti-gros" : "true"
},
"teamDescription" : "Chaud pour un raid"
}
}}
When I try to retrieve a team I would like to have a json like this.
"-KBhfH9U3CxQPZiCs5ru" = {
activity = "Raid - Normal - Crota's end",
creationDate = "2016-02-29 16:37:14",
language = "Any language",
leader = "jojo-pti-gros",
maxPlayers = 6,
mic = true,
minimumLight = 55,
players = {
"jojo-pti-gros" = {
"creationDate" = "2016-02-26 15:50:56",
"level" = "2",
"light" = "2",
"type" = "Titan"
}
},
teamDescription = "Chaud pour un raid"
}
But I have this json instead
"-KBhfH9U3CxQPZiCs5ru" = {
activity = "Raid - Normal - Crota's end",
creationDate = "2016-02-29 16:37:14",
language = "Any language",
leader = "jojo-pti-gros",
maxPlayers = 6,
mic = true,
minimumLight = 55,
players = {
"jojo-pti-gros" = true
},
teamDescription = "Chaud pour un raid"
}
My code right now
let fb = Firebase(url:myUrl)
fb.childByAppendingPath("/Teams").observeSingleEventOfType(.Value, withBlock: { teams in
var teamsArray: [Team] = []
if (teams.value is NSNull) {
print("no teams found")
} else {
print(teams.value)
for team in teams.value as! NSDictionary {
let newTeam = Team()
newTeam.dictionaryToTeam(team.value as! NSDictionary)
newTeam.id = String(team.key)
for player in newTeam.playerDictionary as NSDictionary {
GuardianAPI.sharedInstance.getGuardianByGamertagAndID(player.key as! String, id: player.value as! String, handler: {newGuardian in
newTeam.playerDictionary.setValue(newGuardian, forKey: player.key as! String)
})
}
teamsArray.append(newTeam)
}
handler(true)
}
})
I don't know how to make a query with juncture like SQL. Can somebody know how to resolve this ?
There are a couple of directions to go with this.
One option is to read in your team node, separate out the player refs into perhaps an array and then iterate over the array to get each player ref and load each player with a separate Firebase call
A better option is to store a reference back to the team in each player's node
"Players" : {
"jojo-pti-gros" : {
team: "-KBhfH9U3CxQPZiCs5ru"
}
Observe the teams node to load in the team data then then query the players node for all players that have a child: team = "-KBhfH9U3CxQPZiCs5ru"
Firebase *playersRef = [myRootRef childByAppendingPath:@"Players"];
FQuery *query1 = [playersRef queryOrderedByChild:@"team"];
FQuery *query2 = [query1 queryEqualToValue:@"-KBhfH9U3CxQPZiCs5ru"];
[query2 observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
NSLog(@"key: %@ %@", snapshot.key, snapshot.value);
}];
The result will be a snapshot filled with all of the players that belong to the team.
这篇关于如何在一个查询Firebase中检索多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!