需要重新构建JSON数据库或进行单个查询(Firebase) [英] Need to restructure JSON database or make single query(Firebase)

查看:106
本文介绍了需要重新构建JSON数据库或进行单个查询(Firebase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  users {
profile-data我的JSON树中有两个分支, {
user-key {
//这里是用户个人资料字段,如昵称和其他
}
......
}
个朋友{
user-key {
friend's-user-key:true
another-friend's-user-key:true
......
}
}
}

问题是要获取所有用户的配置文件数据,是任何用户的朋友。我没有找到方法来获取所有这些数据的查询,所以我需要写这样的东西:

  usersFriendsRef。 (DataSnapshot child:dataSnapshot.getChildren()){$ b public void onDataChange(DataSnapshot dataSnapshot){
) $ b String userId = child.getKey();
usersProfileDataRef.child(userId).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
////只有在这里我可以得到用户的配置文件数据

$ b @Override
public void onCancelled(DatabaseError databaseError){

}
});
}

$ b @Override
public void onCancelled(DatabaseError databaseError){
}
});

所以问题是我需要提出很多请求来逐个获取好友的个人资料数据,所以我需要重新调整我的数据库或找到方法来写这个作为1查询

解决方案

这是正确的方式,你可以做到这一点。嵌套查询没有问题。你需要找到所有这些ID然后基于它们来查询。我可以移动 profile-data - >下的 friends user-key 创建一个查询,但知道在Firebase中最好让数据尽可能平坦,这不是一个解决方案。



请记住不要忘记删除监听器,当它不再需要时,像这样:
$ b $ pre $ databaseReference.removeEventListener(valueEventListener );

希望有帮助。


I have 2 branches in my JSON tree, which look like this:

users {
    profile-data {
           user-key {
                //here is users profile fields like nickname and other
           }
           ......
    }
    friends {
        user-key {
            friend's-user-key:true
            another-friend's-user-key:true
            ......
        }
    }
}

The problem is to fetch all users-profile data of users, that are friends of any user. I didn't find the way to fetch all this data by 1 query so I need to write something like this:

usersFriendsRef.child("userId").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    String userId = child.getKey();
                     usersProfileDataRef.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                        ////Only here i can get user's profile data    
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

So the problem is that I need to make many requests to fetch friends' profile data one by one, so I need to restructure my database or find the way to write this as 1 query

解决方案

That's the correct way in which you can achieve this. There's no problem with nested queries. You need to find all those id's and then based on them, to query. I could move the friends node under profile-data -> user-key to create a single query but knowing that in Firebase is best to have the data as flatten as possible, it's not a solution.

Keep in mind not to forget to remove the listener, when it's no more needed, like this:

databaseReference.removeEventListener(valueEventListener);

Hope it helps.

这篇关于需要重新构建JSON数据库或进行单个查询(Firebase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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