交易可以用于收款吗? [英] Can transaction be used on collection?

查看:70
本文介绍了交易可以用于收款吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firestore,并尝试通过使用事务删除Flutter应用中的竞争条件.

I am use Firestore and try to remove race condition in Flutter app by use transaction.

我有一个子集合,最多可添加2个文档.

I have subcollection where add 2 document maximum.

种族条件意味着可能会添加两个以上的文档,因为客户端代码使用的是setData.例如:

Race condition mean more than 2 document may be add because client code is use setData. For example:

Firestore.instance.collection(‘collection').document('document').collection('subCollection’).document(subCollectionDocument2).setData({
  ‘document2’: documentName,
});

我正在尝试使用事务来确保最多添加2个文档.因此,如果在事务运行期间更改了收集(例如将新文档添加到收集中),则事务将失败.

I am try use transaction to make sure maximum 2 document are add. So if collection has been change (For example new document add to collection) while transaction run, the transaction will fail.

但是我读过文档,并且看来交易在竞态条件中使用更多,其中在文档中设置字段,而不是在子集合中添加文档.

But I am read docs and it seem transaction use more for race condition where set field in document, not add document in subcollection.

例如,如果尝试实施:

Firestore.instance.collection(‘collection').document('document').collection('subCollection').runTransaction((transaction) async {

}),

给出错误:

错误:未为类'CollectionReference'定义方法'runTransaction'.

error: The method 'runTransaction' isn't defined for the class 'CollectionReference'.

事务可以用于监视更改为子集合吗?

Can transaction be use for monitor change to subcollection?

有人知道其他解决方案吗?

Anyone know other solution?

推荐答案

事务可以用于监视更改为子集合吗?

Can transaction be use for monitor change to subcollection?

Firestore中的事务通过所谓的比较并交换操作进行工作.在事务中,您从数据库中读取文档,确定其当前状态,然后基于该状态设置其新状态.完成整个事务后,您会将整个当前状态和新状态文档包发送到服务器.然后,服务器检查存储层中的当前状态是否仍然与客户端开始时的状态匹配,如果是,它将提交您指定的新状态.

Transactions in Firestore work by a so-called compare-and-swap operation. In a transaction, you read a document from the database, determine its current state, and then set its new state based on that. When you've done that for the entire transaction, you send the whole package of current-state-and-new-state documents to the server. The server then checks whether the current state in the storage layer still matches what your client started with, and if so it commits the new state that you specified.

认识到这一点,监视事务中的整个集合的唯一方法是将该集合中的所有文档读入事务中.尽管从小规模上来说这在技术上是可行的,但效率可能很低,而且我从未见过在实践中做到这一点.再说一次,对于您集合中的两个文档,在事务中简单地读取它们可能是完全可行的.

Knowing this, the only way it is possible to monitor an entire collection in a transaction is to read all documents in that collection into the transaction. While that is technically possible for small collections, it's likely to be very inefficient, and I've never seen it done in practice. Then again, for just the two documents in your collection it may be totally feasible to simply read them in the transaction.

请记住,尽管交易仅确保一致的数据,但并不一定限制恶意用户的操作.如果要确保集合中的文档数量不超过两个,则应查看服务器端机制.

Keep in mind though that a transaction only ensures consistent data, it doesn't necessarily limit what a malicious user can do. If you want to ensure there are never more than two documents in the collection, you should look at a server-side mechanism.

最简单的机制(从基础架构角度出发)是使用Firestore的服务器端安全规则,但是我不认为这些规则会限制集合中文档的数量,正如Doug在他对在Firestore规则中限制子集合中的许多文档.

The simplest mechanism (infrastructure wise) is to use Firestore's server-side security rules, but I don't think those will work to limit the number of documents in a collection, as Doug explained in his answer to Limit a number of documents in a subcollection in firestore rules.

在这种情况下,最可能的解决方案是(如Doug所建议的)使用Cloud Functions将文档写入子集合中.这样,您可以简单地拒绝来自客户端的直接写操作,并在需要在受信任环境中运行的Cloud Functions代码中实施所需的任何业务逻辑.

The most likely solution in that case is (as Doug also suggests) to use Cloud Functions to write the documents in the subcollection. That way you can simply reject direct writes from the client, and enforce any business logic you want in your Cloud Functions code, which runs in a trusted environment.

这篇关于交易可以用于收款吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆