Firebase安卓计数的孩子和存储在一个变量 [英] Firebase android count children and store it in a variable

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

问题描述

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

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

}

@Override
public void onCancelled(DatabaseError databaseError){

$ b});

包括mCount = dataSnapshot.getChildren似乎不起作用。解决方案

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



最简单的方法是查看当我们放置日志记录:

$ p $ System.out.println(在添加监听器之前);
mRefReg.addValueEventListener(new ValueEventListener(){
public void onDataChange(DataSnapshot dataSnapshot){
System.out.println(onDataChange);
}
public void onCancelled(DatabaseError databaseError){}
});
System.out.println(添加监听器之后);

与您的直觉告诉您的相反,这将是:


在添加监听器之前

添加监听器之后

<在onDataChange中

原因是(正如我在开始时所说的那样),Firebase异步地从数据库中获取数据。一个有趣的方式来实现这一点,就是要改变你的数据(例如在你的 Firebase数据库控制台)。当你做这个改变的时候, onDataChange 会被再次调用并打印另一个:


在onDataChange中


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

  int count = -1; 
System.out.println(在添加监听器之前,count =+ count);
mRefReg.addValueEventListener(new ValueEventListener(){
public void onDataChange(DataSnapshot dataSnapshot){$ b $ count = dataSnapshot.getChildrenCount();
System.out.println(in onDataChange ,count =+ count);
}
public void onCancelled(DatabaseError databaseError){}
});
System.out.println(添加监听器后,count =+ count);

因为输出将会是:


添加监听器之前:count = -1

添加监听器之后:count = -1

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

我发现处理这个最好的技巧异步加载是重构的问题。不要说首先我们得到点数,然后我们做xyz,试着把它定义为每当我们点数时,用它做xyz。这意味着您需要将需要计数的代码移动到 onDataChange()

  mRefReg.addValueEventListener(new ValueEventListener(){
public void onDataChange(DataSnapshot dataSnapshot){
int count = dataSnapshot.getChildrenCount();
// TODO:显示UI中的计数
}
public void onCancelled(DatabaseError databaseError){}
});

如果你这样实现,UI中的计数就会被更新,数据库发生变化。


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

        }
    });

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

解决方案

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:

Before adding listener

After adding listener

In 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:

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

Because the output will be:

Before adding listener: count=-1

After adding listener: count=-1

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

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

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

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

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