亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號