Flutter、Firestoreで500件以上のbatch処理
結果
Future deletePostsOfCurrentUser(User? currentUser) async {
await FirebaseFirestore.instance
.collection('posts')
.where('uid',isEqualTo: currentUser!.uid)
.get()
.then((qshot) {
WriteBatch batch = FirebaseFirestore.instance.batch();
final docs = qshot.docs;
int index = 0;
docs.forEach((DocumentSnapshot doc) async {
if ((index + 1) % 500 == 0) {
// commit by 500 and initialize batch instance
await batch.commit();
batch = FirebaseFirestore.instance.batch();
}
batch.delete(doc.reference);
index++;
});
// last commit
return batch.commit();
});
}