How to retrieve multiple data in one query Firebase

前端 未结 1 1936
时光说笑
时光说笑 2021-01-07 05:03

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\" : {
         


        
相关标签:
1条回答
  • 2021-01-07 05:25

    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.

    0 讨论(0)
提交回复
热议问题