Firebase Data Desc在Android中排序 [英] Firebase Data Desc Sorting in Android

查看:182
本文介绍了Firebase Data Desc在Android中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了所有Internet,Firebase API,因此无法找到我的问题的答案。

我将数据存储在Firebase存储中。



对象注释与属性时间戳。当我将数据从设备推送到Firebase时,我正在使用currentTime填充 timestamp ,并存储在 long 数据类型中。

当我使用 firebaseRef.orderByChild(timestamp)。检索数据时,limitToLast(15)我预料到。



我甚至玩过规则,没有结果:

 <$ c $ 
rules:{
.read:true,
.write:true,
.indexOn:streetrate,
streetrate:{
.indexOn:.value
}
}
}
String 数据类型中尝试存储 timestamp ,同一个问题。
$ b $ p感谢您的帮助。

解决方案

Firebase可以订购商品( limitToFirst())或最后的N个项目( limitToLast())以升序排列, code>)。没有办法表明你想要降序的项目。



有两种选择来获得您想要的行为:


  1. 使用Firebase查询获取正确的数据,然后重新排序客户端


  2. 添加一个具有递减值的字段到数据




  3. 对于后一种方法,通常会有一个倒置的时间戳。

      -1 * new Date()。getTime(); 


    I searched all Internet, Firebase APIs, SO and can't find answer to my question.

    I am storing data in Firebase storage.

    Object Comment with attribute timestamp. When I push data from device to Firebase I'm populating timestamp with currentTime and store in long data type.

    When I do retrieving data with firebaseRef.orderByChild("timestamp").limitToLast(15) result is not sorting how I expected.

    I even played around with rules and no result:

    {
        "rules": {
            ".read": true,
            ".write": true,
            ".indexOn": "streetrate",
            "streetrate": {
              ".indexOn": ".value"
            }
        }
    }
    

    I tried store timestamp in String data type, same issue.

    Thanks for help.

    解决方案

    Firebase can order the items in ascending order by a given property and then returns either the first N items (limitToFirst()) or the last N items (limitToLast()). There is no way to indicate that you want the items in descending order.

    There are two options to get the behavior you want:

    1. Use a Firebase query to get the correct data, then re-order it client-side

    2. Add a field that has a descending value to the data

    For the latter approach, it is common to have a inverted timestamp.

    -1 * new Date().getTime();
    

    这篇关于Firebase Data Desc在Android中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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