Skip to content Skip to sidebar Skip to footer

Having Issue Retrieving Correct Information From Firebase With Multiple Lookups

Below is the code I implemented which works but its assigning the last Firebase child 'PoolName' and 'PoolId' to all three (3) results when they should be different!? playerInPoolR

Solution 1:

UsersuserId1userName :peteruserId2userName :johnPoolspoolId1poolName :pool1poolId2poolName :pool2GamesgameId1gameName :game1gameId2gameName:game2UserPoolpoolId1userName:peterpoolId2userName:johnGamePoolgameIdpoolId:poolId1gameName :game1

You can do the following database, parent node including Users, Pools, and Games. Then you can do two nodes, one called UserPool that can include each all the users that are in each pool, and the other called GamePool that can include game name and pool info.

FirebaseDatabase database       = FirebaseDatabase.getInstance();
DatabaseReference userReference = database.getReference("UserPool");
DatabaseReference gameReference = database.getReference("GamePool");

userReference.orderByKey().equalTo(poolId).addValueEventListener(newValueEventListener() {
 @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
  for(DataSnapshotds: dataSnapshot.getChildren()){
    String keyPool = ds.getKey();
    String userId  = ds.child("userName").getValue().toString();

    gameReference.orderByChild("poolId").equalTo(keyPool).addValueEventListener(newValueEventListener() {
          @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
          for(DataSnapshot datas : dataSnapshot.getChildren()){
            String gameId   = datas.getKey();
            String gameName = datas.child("gameName").getValue().toString();
       }
     }
          @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
    });

   }
 }
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
  }
});

Here you put the reference at child UserPool and retrieve the poolId and the userNames, you can also add other info and retrieve them here.

Then you use a nested valueEventListener that refers to the node gamePool and using the key that you retrieved (which is the poolId), you create a query andorderByChild("poolId").equalTo(keyPool) and retrieve the game name, and other info that you can add there like game info...

Post a Comment for "Having Issue Retrieving Correct Information From Firebase With Multiple Lookups"