Having issue retrieving correct information from Firebase with multiple lookups

試著忘記壹切 提交于 2020-01-05 05:38:25

问题


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!?

playerInPoolReference = mFirebaseDatabase.getReference("PlayerInPool").child(userID);

playerInPoolReference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if(dataSnapshot.exists()) {

                for (DataSnapshot poolSnapshot : dataSnapshot.getChildren()) {

                    String poolID = poolSnapshot.getKey();

                    poolReference = mFirebaseDatabase.getReference("Pools").child(poolID);

                    poolReference.addListenerForSingleValueEvent(new ValueEventListener() {

                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapShot) {

                            if (snapShot.exists()) {

                                pID = snapShot.getKey();

                                String gameID = snapShot.child("GameId").getValue().toString();
                                //
                                pName = snapShot.child("PoolName").getValue().toString();

                                gameReference = mFirebaseDatabase.getReference("Games").child(gameID);

                                gameReference.addListenerForSingleValueEvent(new ValueEventListener() {

                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot snap) {

                                        if (snap.exists()) {

                                            Pools pool = snap.getValue(Pools.class);

                                            pool.setPoolId(pID);
                                            pool.setPoolName(pName);
                                            //
                                            poolList.add(pool);

                                            adapter = new PoolAdapter(getActivity(), poolList);

                                            recyclerView.setAdapter(adapter);

                                        } 

                                    }

                                    @Override
                                    public void onCancelled(@NonNull DatabaseError error) {

                                    }

                                });

                            } 

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }

                    });

                } // End for loop

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });

Does anyone know or have they implemented a solution where I wish to get the list of Groups (ex. Pools in my case) that a particular user is in?

In my case I check for the pools that the player is in, then need to access the Pool Node to get the Pool info, which included the particular game id , and then lastly get the associated Game info so that I can display to the player.

The Firebase structure is as follows:

Users
    userId1
       userName : peter
    userId2
       userName : john

Pools
    poolId1
       poolName: pool1
       etc....

    poolId2
       poolName: pool2

 Games
    1000
       gameName: game1
    1001
       gameName: game2


 PlayerInPool
    UserId
       12345: true
       54321: true
       etc...

    UserId:
       12345: true

The ISSUE is that it sets the following:

pool.setPoolId(pID);
pool.setPoolName(pName);

to the last values in the list of pools!?


回答1:


Users
 userId1
     userName : peter
 userId2
     userName : john


Pools
  poolId1
    poolName : pool1
  poolId2
    poolName : pool2

Games
  gameId1
     gameName : game1
  gameId2
      gameName: game2

UserPool
    poolId1
      userName: peter
    poolId2
      userName: john

GamePool
    gameId
      poolId: poolId1
      gameName : 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(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnapshot ds: dataSnapshot.getChildren()){
    String keyPool = ds.getKey();
    String userId  = ds.child("userName").getValue().toString();

    gameReference.orderByChild("poolId").equalTo(keyPool).addValueEventListener(new ValueEventListener() {
          @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
          for(DataSnapshot datas : dataSnapshot.getChildren()){
            String gameId   = datas.getKey();
            String gameName = datas.child("gameName").getValue().toString();
       }
     }
          @Override
       public void onCancelled(DatabaseError databaseError) {}
    });

   }
 }
@Override
public void onCancelled(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...



来源:https://stackoverflow.com/questions/55433036/having-issue-retrieving-correct-information-from-firebase-with-multiple-lookups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!