getChildrenCount()如何在后台工作? [英] How getChildrenCount() works in the background?

查看:101
本文介绍了getChildrenCount()如何在后台工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我可以说超过5000个用户,现在,如果我在主父节点中使用getChildrenCount()来获得多少人

In my database I have lets say more than 5000 users, now if I use getChildrenCount() in the main parent node to get how many people do I have

我知道getChildrenCount会返回一个带有父级中子级数量的long,但这会如何影响性能?

I know that getChildrenCount returns a long with the quantity of children inside a parent but, how this affects performance ?

几个月前,我制作了一个应用程序来管理用户,该应用程序比通过Web控制台更有效地管理用户,因为该页面变得静态并且用户不断增长.

Months ago I made an app to manage users more efficient than doing it from the web console, since the page became static and the users growth.

现在,我想知道是否每次我请求用户数量时,该应用程序都在遍历5000+个用户,并对每个用户进行计数,所以我在浪费读取资源.

Now, I want to know if every time I request the quantity of users the app is looping through the 5000+ users and count it for each one, so I'm wasting read resources.

我的问题:

getChildrenCount()是否将我的用户的最新检索数量存储在某个地方?
每次我运行我的应用以在每个孩子中用+ =计数时,getChildrenCount()都会执行吗?

Does getChildrenCount() stores somewhere the latest retrieved number of my users ?
Does getChildrenCount() executes each time I run my app to count with += in each child ?

我认为这样做的最佳实践是运行一次getChildrenCount,然后将这些用户存储在一个节点中,让我们说userQuantity = 5000,然后在每次有新用户进入(带有事务处理)时将1加到该节点上,所以在我的应用程序管理器中,我只查询一个节点即可获得数量,它比再次遍历所有父节点来获取用户更有效

I think the best practice of doing this is runing getChildrenCount one time and then store those users in one node lets say userQuantity = 5000 and then just add 1 to that node each time a new user comes in (with transactions), so in my app manager I just query a single node obtaining the quantity and it's more efficient than looping through all the parent node again to get the users

推荐答案

使用DataSnapshot的getChildrenCount()方法时,并不表示您正在遍历整个DataSnapshot对象.相反,如果您使用的是DataSnapshot的getChildren()方法,那么您正是在这样做.让我们举个例子,假设您有一个名为users的节点,正如您所说,该节点可以容纳5000多名用户.

When you are using DataSnapshot's getChildrenCount() method, it doesn't mean that you are looping through the entire DataSnapshot object. Instead, if you are using DataSnapshot's getChildren() method you are precisely doing that. Let's take an example, let's say you have a node named users which holds as you say, over 5000 users.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long numberOfUsers = dataSnapshot.getChildrenCount(); // Count the number of users

        //This is called iteration
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            // Get user object on every iteration
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(valueEventListener);

因为Firebase数据库中的每个节点都是Map,所以

Because every node in a Firebase database is a Map, DataSnapshot can also be called a Map.

DataSnapshot实例包含Firebase数据库位置中的数据.每次读取数据库数据时,您都会以DataSnapshot的形式接收数据.

A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.

因此,如果您要将DataSnapshot对象转换为Map,则以下代码行将很好地工作:

So if you want to cast a DataSnapshot object to a Map, the following line of code will work perfectly fine:

Map<String, Object> map = (Map<String, Object>) dataSnapshot;

因此,在DataSnapshot对象上调用getChildrenCount()就像在Map上调用size()方法一样,该方法在内部对它包含的对象数进行计数.

So calling getChildrenCount() on a DataSnapshot object is like calling size() method on a Map, which internally counts the number of objects it contains.

我在浪费阅读资源

I'm wasting read resources

在新的Cloud Firestore数据库中可用,根据您对Firestore数据库执行的CRUD操作数向您收费.如果您有兴趣,请 Cloud Firestore中集合下方的文档集合.如您所见,在经典计数操作+=旁边,我们还有task.getResult().size().我还提到过,存储计数器的最佳实践是将它们存储在Firebase实时数据库中.因此,我认为,请继续进行此操作并将其分别存储.

This is available in case of the new Cloud Firestore database, where you are charged according to the number of CRUD operations you perform against the Firestore database. If you are interested, this is how you can count the number of documents beneath a collection in Cloud Firestore. As you can see, beside the classic count opertion +=, we also have task.getResult().size(). I also have mentioned that the best practice to store counters is to store them in Firebase real-time database. So in my opinion, go ahead with that and store them separately.

如果您认为自己的应用将增长很快,那么另一种方法是使用云功能每次在users节点中添加或删除用户时更新用户计数器.这种技术适用于大型数据集.

If you think that your app will grow very fast, another way would be to use Cloud Functions to update the user counter every time you add or delete a users from the your users node. This tehnique works very well for big datasets.

并回答您的问题:

getChildrenCount()是否将我的用户的最新检索数量存储在某个地方?每当我运行我的应用以在每个孩子中用+ =进行计数时,getChildrenCount()是否会执行?

Does getChildrenCount() stores somewhere the latest retrieved number of my users ? Does getChildrenCount() executes each time I run my app to count with += in each child ?

如果您认为最新检索到的号码以某种方式存储在磁盘/内存中,则不会.每次打开应用程序时,都会调用getChildrenCount()方法来获取最新的用户数.如果要实时获取该号码,则应在users参考上附加一个侦听器.

If you are thinking that the latest retrieved number is stored somehow on the disk/memory, no. Everytime you are opening the app, getChildrenCount() method will be called to get the latest number of users. If you want to have that number in real-time, then you should attach a listener on users reference.

这篇关于getChildrenCount()如何在后台工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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