为了账号安全,请及时绑定邮箱和手机立即绑定

Firebase .indexOn 未按预期工作

Firebase .indexOn 未按预期工作

慕森卡 2023-02-16 16:22:14
我有一个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 dataquery = 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 dataquery = 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"}...}
查看完整描述

1 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

您似乎误解了什么.indexOn,以及它与orderByChild("time").

要从contentordered by(也可能是 filtered on)中检索子节点time,您将始终需要调用orderByChild("time").

如果您定义一个索引(使用".indexOn": "time"),那么排序和过滤将在服务器上完成。

没有索引,下的所有数据都content将下载到客户端,排序/过滤将在客户端执行。

所以.indexOn不能替代orderByChild. 相反,两者携手合作以执行高效的数据排序和过滤。


查看完整回答
反对 回复 2023-02-16
  • 1 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号