我想在螢屏上顯示前 25 名玩家的串列(從 Firestore 獲取),目前這是我實作它的方式:
late final Stream<QuerySnapshot> _mainScoreStream;
@override
void initState() {
futureAd = fetchAd();
_mainScoreStream = FirebaseFirestore.instance
.collection('users')
.orderBy('current_score', descending: true)
.where('current_score', isGreaterThan: 0)
.limit(25)
.snapshots();
super.initState();
}
@override
Widget build(BuildContext context) {
// Used to make text size automaticaly sizeable for all devices
final double unitHeightValue = MediaQuery.of(context).size.height * 0.01;
final user = Provider.of<UserModels?>(context);
return SafeArea(
child: StreamBuilder<QuerySnapshot>(
stream: _mainScoreStream,
// ignore: missing_return
builder: (context, snapshot) {
if (snapshot.hasData) {
return Expanded(
child: ListView.builder(
physics: BouncingScrollPhysics(),
itemBuilder: (context, index) {
DocumentSnapshot data = snapshot.data!.docs[index];
return LeaderboardCard(
currentScore: data['current_score'].toString(),
name: data['name'],
index: index,
isCurrentUser: data.id == user!.uid,
);
},
itemCount: snapshot.data!.docs.length,
),
);
} else if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
strokeWidth: 2.0,
),
);
}
return Container();
},
),
);
}
}
排行榜每天更改一次,但是通過此實作,我可以為每位用戶額外獲得 25 次讀取。
有沒有更有效的方法來獲取這些資料,或者因為我每個用戶的每日閱讀量約為 30 ,所以這樣可以嗎?
編輯:我知道過度優化我的讀/寫不是一個好習慣,但目前基于 Firebase 定價計算器,這可能會導致大量的日常閱讀,所以不知道如何去做,我總是可以減少限制或完全洗掉排行榜
uj5u.com熱心網友回復:
如果您每天更改一次排行榜,您可以:
計算受信任系統(您的開發機器、您控制的服務器或 Cloud Functions/Cloud Run)上的排行榜內容,并將其存盤在 Firestore 中的單獨檔案中。然后每個客戶只需閱讀該檔案即可獲得整個排行榜。
每天在受信任的系統上創建包含查詢結果的資料包,然后通過更便宜的系統(例如 Cloud Storage 或什至再次作為 Firestore 中的檔案)將其分發給您的用戶。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/409508.html
標籤:
上一篇:firebase.auth().onAuthStateChanged登錄后未執行?
下一篇:如何在Android中打破每個?
