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

2023-12-11

我是 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" : "[email protected]",
          "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那样使用juncture进行查询。 有人可以知道如何解决这个问题吗?


有几个方向可以实现这一点。

一种选择是读入您的团队节点,将玩家引用分离到一个数组中,然后迭代该数组以获取每个玩家引用并使用单独的 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);

}];

结果将是包含属于该团队的所有球员的快照。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在一次查询中检索多个数据 Firebase 的相关文章

随机推荐