Android Firebase奇怪的limitToLast(1)行为 [英] Android Firebase weird limitToLast(1) behaviour

查看:59
本文介绍了Android Firebase奇怪的limitToLast(1)行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase for Android,并且遇到了 limitToLast()查询的某些错误行为.我想做的是获取列表的最后一个子节点,并遍历此节点的其他子节点:

I'm using Firebase for Android and faced some misbehaviour of limitToLast() query. What I'm trying to do, is to get the last child of list, and iterate through other children of this node:

database.getReference().child("draws").limitToLast(1).addValueEventListener(new ValueEventListener() { 
@Override                                                                                                       
public void onDataChange(DataSnapshot dataSnapshot) {                                                           
    for(DataSnapshot snap: dataSnapshot.getChildren()){
        //Some work                                                                                             
        snap.getValue()                                                  
    }                                                                                                           
}                                                                                                               

@Override                                                                                                       
public void onCancelled(DatabaseError databaseError) {  
        //Some error handling                                                                                                                                                     
}                                                                                                               
});                                                                                                                 

让我们假设,我正在使用limitToLast(1)来获得子 draws/2 (请参阅下面的数据库快照),这是绘制列表中的最后一个.之后,我使用dataSnapshot.getChildren()获取 draws/2 节点的子级列表.在遍历dataSnapshot.getChildren()列表后,我应该得到什么:"85","86","60","7"作为单独的值,并在循环中使用snap.getValue().toString().

Let's assume, I'm using limitToLast(1) in order to get the child draws/2(refer to the database snap below), which is the last in a draws list. And after, I'm using dataSnapshot.getChildren() to get the list of children of draws/2 node. What I should get after iterating through the dataSnapshot.getChildren() list: "85", "86", "60", "7" as separate values with snap.getValue().toString() in the loop.

我实际上得到的是上一个孩子的价值,而没有接触它的孩子.结果:"[85,86,60,7]".看起来dataSnapshot.getChildren()不会获取节点 draws/2 的子节点,而只会获取该节点的整个值.

What I actually get, is the value of last child without accessing to its children. Result: "[85, 86, 60, 7]". It looks like dataSnapshot.getChildren() doesn't get children of node draws/2, but just gets a whole value of the node.

作为测试,我试图摆脱limitToLast(1)查询并用child("2")代替,一切正常.

As a test, I tried to get rid of limitToLast(1) query and substitute with child("2") and everything worked fine.

我可能会误解了limitToLast()的功能,但是,如果为true,如何访问最后一个节点的子代?

It is possible that I misunderstood the functionality of limitToLast(), however, if it is true, how can I access children of last node?

推荐答案

对Firebase数据库执行查询时,可能会有多个结果.因此,快照包含这些结果的列表.即使只有一个结果,快照也会包含一个结果的列表.

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

因此,您的代码将需要 first 首先(通过循环)找到draws/2子级,然后 then 遍历子级:

So your code will need to first find the draws/2 child (by looping) and then loop over the children:

public void onDataChange(DataSnapshot dataSnapshot) {                                                           
    for(DataSnapshot snap: dataSnapshot.getChildren()){
        System.out.println(snap.getKey()); // prints 2
        for (DataSnapshot snap2: snap.getChildren()){
            //Some work                                                                                             
            snap.getValue()                                                  
        }
    }                                                                                                           
}

使用addChildEventListener处理查询结果通常会更容易,因为这样您将为每个孩子得到一个单独的onChildAdded调用(因此可以跳过多余的循环).

It's typically easier to handle query results by using addChildEventListener, because then you'll get a separate call to onChildAdded for each child (and can thus skip the extra loop).

顺便说一句:文档对于Firebase数据库不使用数组,甚至不使用建议不要使用数组.忽略它们,后果自负.

Btw: there are a lot of reasons why the documentation for the Firebase Database doesn't use arrays and even recommends against using arrays. Ignore them at your own peril.

这篇关于Android Firebase奇怪的limitToLast(1)行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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