如何获取firebase数据库中的所有孩子的数据? [英] How to get all child's data in firebase database?

查看:158
本文介绍了如何获取firebase数据库中的所有孩子的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个firebase数据库





我需要全部用户的电话号码,我应该使用哪个监听器来获取所有的孩子?



每个用户都添加了一个用户ID作为对象名称的对象,我需要在不知道用户ID的情况下检索这些对象我搜索了文档,它与 DataSnapshot 有关,但是我不能没有一个监听器就没有 DataSnapshot !找到一个DataSnapshout是否正确,或者我还要用别的东西

解决方案

首先检索用户数据快照。
$ b

  //在你的users根节点获取datasnapshot 
DatabaseReference ref = FirebaseDatabase.getInstance().getReference()。child(users );
ref.addListenerForSingleValueEvent(
new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
//获取datasnapshot中的用户地图
)collectPhoneNumbers((Map< String,Object>)dataSnapshot.getValue());
}

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

然后循环访问用户,访问他们的地图并收集手机字段。 b

  private void collectPhoneNumbers(Map< String,Object> users){

ArrayList< Long> phoneNumbers = new ArrayList<>(); (Map.Entry< String,Object>条目:users.entrySet()){

//遍历每个用户,忽略他们的UID
/获取用户地图
Map singleUser =(Map)entry.getValue();
//获取手机字段并追加到列表
phoneNumbers.add((Long)singleUser.get(phone));
}

System.out.println(phoneNumbers.toString());

这个侦听器在显式调用时只会检索数据快照。除了用户节点之外,请考虑在allPhoneNumbers节点下存储一个数字列表。这将使您的数据快照收集所有数字时更轻,更容易处理。如果你有数百个用户,那么用户数据快照会过大,allPhoneNumbers列表会更有效率。



上面的代码已经过测试在您的示例数据库,并确实工作。但是,根据用户的电话实例字段的类型,您的电话字段可能需要转换为字符串或整型。


I have this firebase database

and i need to get all phone numbers of users , which listener shall i use to get all childes?

Every user is added as an object with user-ID as a name of that object, I need to retrieve this objects without knowing that user-ID

I searched the documentation , it's related to DataSnapshot but i couldn't get a DataSnapshot without a listener ! is it correct to seek a DataSnapshout or shall i use something else

解决方案

First retrieve your users datasnapshot.

//Get datasnapshot at your "users" root node
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
    ref.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //Get map of users in datasnapshot
                    collectPhoneNumbers((Map<String,Object>) dataSnapshot.getValue());
                }

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

Then loop through users, accessing their map and collecting the phone field.

private void collectPhoneNumbers(Map<String,Object> users) {

    ArrayList<Long> phoneNumbers = new ArrayList<>();

    //iterate through each user, ignoring their UID
    for (Map.Entry<String, Object> entry : users.entrySet()){

        //Get user map
        Map singleUser = (Map) entry.getValue();
        //Get phone field and append to list
        phoneNumbers.add((Long) singleUser.get("phone"));
    }

    System.out.println(phoneNumbers.toString());
}

This listener will only retrieve the datasnapshot when explicitly called. Consider storing a list of numbers under an "allPhoneNumbers" node in addition to your users node. This will make your datasnapshots lighter and easier to process when collecting all numbers. If you have say hundreds of users, the "users" datasnapshot will be way too big and the "allPhoneNumbers" list will be far more efficient.

The above code was tested on your sample database and does work. However, your phone field may need to be casted to String or int depending on your user's phone instance field's type.

这篇关于如何获取firebase数据库中的所有孩子的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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