Firebase .indexOn 未按预期工作

我有一个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"

}

...

}


慕森卡
浏览 125回答 1
1回答

HUWWW

您似乎误解了什么.indexOn,以及它与orderByChild("time").要从contentordered by(也可能是 filtered on)中检索子节点time,您将始终需要调用orderByChild("time").如果您定义一个索引(使用".indexOn": "time"),那么排序和过滤将在服务器上完成。没有索引,下的所有数据都content将下载到客户端,排序/过滤将在客户端执行。所以.indexOn不能替代orderByChild. 相反,两者携手合作以执行高效的数据排序和过滤。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java