我有一个content从 Firebase 实时数据库中检索到的节点。我利用它.orderByChild("time")来获取按最早时间戳排序的前 5 个博客对象的数据快照。我
我目前正在尝试使用.indexOn : "time",这样我就不必使用了,.orderByChild("time")因为我期望博客对象在检索时已经按后端的时间戳排序。(我将要处理大量的博客对象,所以我想.indexOn : "time"在后端而不是orderByChild("time")在前端使用以提高效率)。目前,.indexOn不起作用,并且数据在检索时未按其时间字段排序。
查询无.indexOn
// this works fine
// startAt is utilized for saving last blog object retrieved to retrieve more data
query =
FirebaseDatabase.getInstance().getReference()
.child("content")
.orderByChild("time")
.startAt(nodeId)
.limitToFirst(5);
使用 .indexOn 查询
// this along with the Firebase rules below does not return the same result as above
// startAt is utilized for saving last blog object retrieved to retrieve more data
query =
FirebaseDatabase.getInstance().getReference()
.child("content")
.startAt(nodeId)
.limitToFirst(5);
火力地堡规则:
{
"rules": {
".read": true,
".write": true,
"content" : {
".indexOn": "time"
}
}
}
Firebase 中数据的 JSON 结构:
"content" : {
"blog-0001" : {
"content_id" : "blog-0001",
"image" : "someimage",
"time" : 13,
"title" : "title1",
"type" : "blog"
},
"blog-0002" : {
"content_id" : "blog-0002",
"image" : "someimage",
"time" : 12,
"title" : "title2",
"type" : "blog"
},
"blog-0003" : {
"content_id" : "blog-0003",
"image" : "someimage",
"time" : 11,
"title" : "title3",
"type" : "blog"
},
"blog-0004" : {
"content_id" : "blog-0004",
"image" : "someimage",
"time" : 15,
"title" : "title4",
"type" : "blog"
}
...
}
HUWWW
相关分类