如何在Firebase中执行多位置更新时使用交易? [英] How can I use a transaction while performing a multi-location update in Firebase?

查看:146
本文介绍了如何在Firebase中执行多位置更新时使用交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Firebase数据库中,我需要两次写入两个位置。我有两个位置的规则,以确保用户不能写入那里,同时写入到其他位置。



写入这些位置之一需要是递增/递减。当然,这必须通过一个事务来完成,否则我不能保证用户不会覆盖其他用户的同时递增/递减更新到同一个节点。

问题是,我找不到有关将多位置更新与事务组合在一起的文档。这是不可能做到的?

解决方案

Transactions在单个节点上运行。这意味着如果你想运行一个多位置事务,你将必须在树中的最低级共享节点上运行这个事务。这几乎不是一个好主意。



另一种方法是使用服务器端安全规则来保护您的多位置写入。有关此示例,请参阅 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.

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屋!

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