【问题标题】:Firebase's limitToLast() method not working as expectedFirebase 的 limitToLast() 方法未按预期工作
【发布时间】:2016-04-27 18:05:25
【问题描述】:

我向已填充的 Firebase 节点添加了一个侦听器。我只想获取新同步的数据。我在 ref 上调用 set limitToLast(1),但它从节点中检索所有数据。该代码设置为向多个 refs 添加侦听器,但目前它仅向 1 添加侦听器。

这是我的代码:

                                    //size() == 1
for (int i  = 0; i < firebaseArrayList.size(); i++) { 
            firebaseArrayList.get(i).orderByValue().limitToLast(1);
            firebaseArrayList.get(i).addChildEventListener(this);
        }

当我在代码中使用查询时,有谁知道为什么要为节点中已经存在的每个项目调用 onChildAdded()?

提前致谢

【问题讨论】:

    标签: java android firebase


    【解决方案1】:

    documentation 上面写着:

    Generates a new Query object limited to the last certain number of children.
    

    在您的代码中,您没有使用由limitToLast 生成的对象。

    这是文档中的示例:

    var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
    ref.orderByChild("weight").limitToLast(2).on("child_added", function(snapshot) {
        console.log(snapshot.key());
    });
    

    【讨论】:

    • 感谢您的回复,解决了问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多