Firebase Data Desc Sorting in Android(Android中的Firebase数据描述排序)
问题描述
我将数据存储在 Firebase 存储中.
I am storing data in Firebase storage.
对象 Comment 带有属性 timestamp.当我将数据从设备推送到 Firebase 时,我使用 currentTime 填充 timestamp 并存储在 long 数据类型中.
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.
当我使用 firebaseRef.orderByChild("timestamp").limitToLast(15) 检索数据时,结果未按预期排序.
When I do retrieving the 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"
}
}
}
我尝试将 timestamp 存储在 String 数据类型中,同样的问题.
I tried store timestamp in String data type, same issue.
推荐答案
Firebase 可以按给定属性对项目进行升序排序,然后返回前 N 个项目 (limitToFirst()) 或最后 N 项 (limitToLast()).无法表明您希望项目按降序排列.
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:
使用 Firebase 查询获取正确的数据,然后在客户端重新排序
Use a Firebase query to get the correct data, then re-order it client-side
向数据中添加一个具有降序值的字段
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();
这篇关于Android中的Firebase数据描述排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android中的Firebase数据描述排序
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
