目前我有一個包含用戶串列的集合
在我的管理應用程式中,我有一個按鈕,可讓我根據我設定的當前值更新用戶檔案,這是通過此功能完成的:
onPressed: () async {
var querySnapshots = await collection
.where('current_pick', isEqualTo: _currentValue)
.get();
for (var doc in querySnapshots.docs) {
await doc.reference.update({
'current_streak': FieldValue.increment(1),
'current_score': FieldValue.increment(1),
'rank_up': true,
});
}
},
該功能有效,但它會一一更新所有值,目前還可以,但隨著用戶數量的增加,不確定
我注意到它很少會跳過更新某些用戶的三分之二的值,并且想知道是否有不同的方法來更新值而不會失敗?
uj5u.com熱心網友回復:
聽起來您會想要使用批量寫入,它允許您以原子方式寫入多個檔案。
對于看起來像這樣的代碼:
// Get a new write batch
final batch = db.batch();
// Put the updates into the batch
for (var doc in querySnapshots.docs) {
batch.update(doc.reference, {
'current_streak': FieldValue.increment(1),
'current_score': FieldValue.increment(1),
'rank_up': true,
});
}
// Commit the batch
batch.commit().then((_) {
請注意,批處理寫入最多可以包含 500 個操作,因此如果您可能有超過 500 個檔案要更新,則必須將其拆分為多個批處理寫入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/476478.html
