Firebase android 计数孩子并将其存储在变量中 [英] Firebase android count children and store it in a variable

查看:34
本文介绍了Firebase android 计数孩子并将其存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我可以通过 datasnapshot.getchildren() 计算孩子的数量.但是如果我想将该计数存储在一个变量中,我该怎么办?

In my application I'm able to get children count by datasnapshot.getchildren(). But what should I do if I want to store that count in a variable?

mRefReg.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.i(dataSnapshot.getKey(),dataSnapshot.getChildrenCount()+"Count");

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

包括mCount = dataSnapshot.getChildren"似乎不起作用.

including "mCount = dataSnapshot.getChildren" does not seem to work.

推荐答案

您所看到的可能是来自 Firebase 的数据是异步加载的.第一次遇到这种情况时,这是一个很大的范式转变.但由于大多数现代云 API 都以这种方式工作,因此最好尽快接受它.

What you're seeing is likely the fact that data from Firebase is loaded asynchronously. The first time you encounter this, it's a big paradigm shift. But since most modern cloud APIs work this way, it's best to embrace it as quickly as possible.

我发现的最简单的方法来查看当我们放置一些日志语句时会发生什么:

The easiest way I've found to see what happens when we place some logging statements:

System.out.println("Before adding listener");
mRefReg.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println("In onDataChange");
    }
    public void onCancelled(DatabaseError databaseError) { }
});
System.out.println("After adding listener");

与您的直觉告诉您的相反,此输出将是:

Contrary to what your intuition tells you, the output of this will be:

添加监听器之前

添加监听器后

在 onDataChange

In onDataChange

这样做的原因是(正如我在开始时所说的)Firebase 从数据库中异步获取数据.了解原因的一种有趣方式是更改您的数据(例如在您的 Firebase 数据库控制台).当您进行此更改时,将再次调用 onDataChange 并打印另一个:

The reason for this is (as I said at the start) that Firebase asynchronously gets the data from the database. A fun way to realize why that is, is to make a change to your data (for example in your Firebase Database console). When you make this change, the onDataChange will be invoked again and print another:

在 onDataChange

In onDataChange

不幸的是,这意味着您无法执行以下操作来处理子项计数:

Unfortunately this means that you cannot do the following to handle the children count:

int count = -1;
System.out.println("Before adding listener, count="+count);
mRefReg.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        count = dataSnapshot.getChildrenCount();
        System.out.println("In onDataChange, count="+count);
    }
    public void onCancelled(DatabaseError databaseError) { }
});
System.out.println("After adding listener, count="+count);

因为输出将是:

添加监听器前:count=-1

Before adding listener: count=-1

添加监听器后:count=-1

After adding listener: count=-1

在 onDataChange 中:count=3(假设您有 3 个孩子)

In onDataChange: count=3 (assuming you have 3 children)

我发现处理这种异步加载的最佳技巧是重新定义问题.与其说首先我们得到计数,然后我们用它做 xyz",不如试着把它设计成每当我们得到计数时,用它做 xyz".这意味着您需要将需要计数的代码移入 onDataChange():

The best trick I've found to deal with this asynchronous loading is to reframe the problem. Instead of saying "first we get the count, then we do xyz with it", try framing it as "whenever we get the count, do xyz with it". This means that you need to move the code that requires the count into onDataChange():

mRefReg.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        int count = dataSnapshot.getChildrenCount();
        // TODO: show the count in the UI
    }
    public void onCancelled(DatabaseError databaseError) { }
});

如果你这样实现,每当数据库中的项目数量发生变化时,UI 中的计数就会更新.

If you implement it like this, the count in the UI will be updated whenever the number of items in the database changes.

这篇关于Firebase android 计数孩子并将其存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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