【发布时间】:2023-03-25 09:31:01
【问题描述】:
我正在使用 Firestore,并尝试通过使用事务删除 Flutter 应用中的竞争条件。
我有最多添加 2 个文档的子集合。
竞争条件意味着可能会添加 2 个以上的文档,因为客户端代码使用 setData。例如:
Firestore.instance.collection(‘collection').document('document').collection('subCollection’).document(subCollectionDocument2).setData({
‘document2’: documentName,
});
我正在尝试使用事务来确保最多添加 2 个文档。因此,如果在事务运行时集合发生了变化(例如新文档添加到集合中),事务将失败。
但我读过文档,似乎事务更多地用于在文档中设置字段的竞争条件,而不是在子集合中添加文档。
例如,如果尝试实现:
Firestore.instance.collection(‘collection').document('document').collection('subCollection').runTransaction((transaction) async {
}),
给出错误:
错误:没有为类“CollectionReference”定义方法“runTransaction”。
事务可以用于监控子集合的变化吗?
有人知道其他解决方案吗?
【问题讨论】:
标签: firebase transactions google-cloud-platform flutter google-cloud-firestore