Firebase Data Desc在Android中排序 [英] Firebase Data Desc Sorting in Android
问题描述
我将数据存储在Firebase存储中。
对象 当我使用 我甚至玩过规则,没有结果:注释与属性
$ p $我在$ 时间戳
。当我将数据从设备推送到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>)。没有办法表明你想要降序的项目。
有两种选择来获得您想要的行为:
-
使用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
对于后一种方法,通常会有一个倒置的时间戳。
-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:
For the latter approach, it is common to have a inverted timestamp.
-1 * new Date().getTime();
这篇关于Firebase Data Desc在Android中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!