在通过多次查询从Firebase检索正确的信息时出现问题 [英] Having issue retrieving correct information from Firebase with multiple lookups

查看:54
本文介绍了在通过多次查询从Firebase检索正确的信息时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我实现的代码,该代码可以工作,但是当它们的最后三个Firebase子对象"PoolName"和"PoolId"不同时,它们将它们分配给所有三(3)个结果!?

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?

在我的情况下,我检查玩家所在的池,然后需要访问池节点以获取池信息,其中包括特定的游戏ID,然后最后获取关联的游戏信息,以便我可以显示给玩家.

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.

Firebase的结构如下:

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

推荐答案

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


您可以执行以下数据库,其父节点包括UsersPoolsGames.然后,您可以做两个节点,一个叫UserPool,可以包含每个池中的所有用户,另一个叫GamePool,可以包含游戏名称和池信息.


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) {
  }
});

在此处将引用放在子级UserPool上并检索poolIduserNames,还可以添加其他信息并在此处检索它们.

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.

然后,您使用引用节点gamePool的嵌套valueEventListener,并使用检索到的键(即poolId)创建查询和orderByChild("poolId").equalTo(keyPool)并检索游戏名称,以及您可以在其中添加的其他信息,例如游戏信息...

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...

这篇关于在通过多次查询从Firebase检索正确的信息时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆