OStack程序员社区-中国程序员成长平台

标题: ios - 如何在一个查询中检索多个数据 Firebase [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:41
标题: ios - 如何在一个查询中检索多个数据 Firebase

我是 firebase 和 nosql 的新手。我遵循了构建数据的指南,这就是我的数据的样子

{"Guardians" : {
      "jojo-pti-gros" : {
          "-KBT2ui6wlC6Fgk1c7Xa" : {
              "creationDate" : "2016-02-26 15:50:56",
              "level" : "2",
              "light" : "2",
              "type" : "Titan"
          }
      }
  },
  "layers" : {
      "jojo-pti-gros" : {
          "console" : "S4",
          "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 之类的接合点进行查询。 有人知道如何解决这个问题吗?



Best Answer-推荐答案


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

一种选择是读入您的团队节点,将玩家 refs 分离成一个数组,然后遍历该数组以获取每个玩家 ref 并使用单独的 Firebase 调用加载每个玩家

更好的选择是在每个玩家的节点中存储回团队的引用

 "layers" : {
      "jojo-pti-gros" : {
         team: "-KBhfH9U3CxQPZiCs5ru"
      }

观察teams节点加载球队数据,然后查询所有有 child 的球员的球员节点:team = "-KBhfH9U3CxQPZiCs5ru"

Firebase *playersRef = [myRootRef childByAppendingPath"layers"];
FQuery *query1 = [playersRef queryOrderedByChild"team"];    
FQuery *query2 = [query1 queryEqualToValue"-KBhfH9U3CxQPZiCs5ru"];

[query2 observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

    NSLog(@"key: %@    %@", snapshot.key, snapshot.value);

}];

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

关于ios - 如何在一个查询中检索多个数据 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35720686/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4