在 Firebase 中执行多位置更新时如何使用事务? [英] How can I use a transaction while performing a multi-location update in Firebase?
问题描述
在我的 Firebase 数据库中,我需要一次写入两个位置.我对这两个位置都有规则,以确保用户不能在不同时写入另一个位置的情况下在那里写入.
In my Firebase database, I need two write to two locations at once. I have rules for both locations that ensure that a user can't write there without simultaneously writing to the other location.
对这些位置之一的写入需要递增/递减.当然,这必须通过事务来完成,否则我不能保证用户不会覆盖其他用户对同一节点的同时递增/递减更新.
The write to one of these locations needs to be an increment/decrement. Of course, that has to be done via a transaction, otherwise I can't guarantee that the user isn't overwriting some other user's simultaneous increment/decrement update to that same node.
问题是,我找不到任何关于将多位置更新与事务相结合的文档.这是不可能的吗?
The problem is, I can't find any documentation on combining multi-location updates with transactions. Is this just impossible to do?
推荐答案
没有.交易在单个节点上运行.
No. Transactions function on a single node.
这意味着如果你想运行一个多位置事务,你基本上必须在树中的最低级别共享节点"上运行该事务.这绝不是一个好主意.
That means that if you want to run a multi-location transaction, you will essentially have to run that transaction at the "lowest level shared node" in the tree. That is hardly ever a good idea.
另一种方法是使用服务器端安全规则来保护您的多位置写入.有关此示例,请参阅 是Firebase 数据库快速入门处理计数的方式是否安全?,本质上是使用安全规则构建比较和设置操作.
The alternative would be to secure your multi-location write using server-side security rules. For an example of this, see Is the way the Firebase database quickstart handles counts secure?, which essentially builds a compare-and-set operation using security rules.
这篇关于在 Firebase 中执行多位置更新时如何使用事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!