如何在一个查询Firebase中检索多个数据

时间:2016-03-01 10:40:02

标签: ios json firebase firebase-realtime-database

我是firebase和nosql的新手。我按照指南构建数据,这是我的数据的样子

{"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"
     }
 }}

当我尝试检索一个团队时,我希望有一个像这样的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" = {
            "creationDate" = "2016-02-26 15:50:56",
            "level" = "2",
            "light" = "2",
            "type" = "Titan"
        }
    },
    teamDescription = "Chaud pour 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] = []
        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)
        }
    })

我不知道如何使用类似SQL的连接进行查询。 有人可以知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

有几个方向可以解决这个问题。

一个选项是读入你的团队节点,将玩家引用分成一个数组,然后迭代数组以获得每个玩家参考并通过单独的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);

}];

结果将是一个快照,其中包含所有属于该团队的玩家。