Firestore事务嵌套循环报错:如何实现批量原子读写?
问题
我编写了如下代码,尝试在Firebase Firestore事务中嵌套循环处理文档:
List<QueryDocumentSnapshot> localList2 = [] ; // 列表内包含5个文档 FirebaseFirestore.instance.runTransaction((transaction) async { for (var element in localList2) { DocumentReference userDoc= FirebaseFirestore.instance.collection("users").doc(element.get('userId')); DocumentSnapshot chick = await transaction.get(userDoc); if(chick.exists){ transaction.update(element.get('targetDocRef'),{ 'fieldValue': 'value', }); } } }).then((value){ log.log('done'); }).catchError((error){ log.log(error.toString()); });
运行时出现错误提示:
Transactions require all reads to be executed before all writes.
我未发现读操作前存在写操作,将整个事务放入循环可正常运行,但我需要批量校验并原子性完成所有文档的写入,该如何解决?
解决方案
Firestore事务的核心规则是所有读操作必须在所有写操作之前完成,你的代码在循环中交替执行transaction.get()(读)和transaction.update()(写),每一次循环里先读再写,导致写操作穿插在读操作流程中,直接违反了事务的执行规则,因此触发错误。
正确的处理方式是把读、写操作完全分离:
- 先遍历所有待处理项,完成全部读操作,同时收集所有需要更新的文档引用
- 再统一遍历收集到的待更新列表,执行所有写操作
修改后的代码如下:
List<QueryDocumentSnapshot> localList2 = []; // 包含5个文档 FirebaseFirestore.instance.runTransaction((transaction) async { // 第一步:批量完成读校验,收集待更新文档 final List<DocumentReference> docsToUpdate = []; for (final element in localList2) { final DocumentReference userDoc = FirebaseFirestore.instance.collection("users").doc(element.get('userId')); final DocumentSnapshot userSnapshot = await transaction.get(userDoc); if (userSnapshot.exists) { docsToUpdate.add(element.get('targetDocRef')); } } // 第二步:统一执行所有更新操作 for (final docRef in docsToUpdate) { transaction.update(docRef, { 'fieldValue': 'value', }); } }).then((value) { log.log('done'); }).catchError((error) { log.log(error.toString()); });
这种写法严格遵循了Firestore事务的要求,既能完成批量校验,又能保证所有更新操作的原子性——要么全部执行成功,要么全部回滚。
内容的提问来源于stack exchange,提问作者Jack
相关产品推荐
相关产品推荐

