在Google Firestore中使用交易的弊端 [英] Downside of using transactions in google firestore

查看:86
本文介绍了在Google Firestore中使用交易的弊端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Flutter应用程序,并且正在使用Firebase服务.我只想坚持使用事务,因为我更喜欢一致性而不是简单性.

I'm developing a Flutter App and I'm using the Firebase services. I'd like to stick only to using transactions as I prefer consistency over simplicity.

await Firestore.instance.collection('user').document(id).updateData({'name': 'new name'});

await Firestore.instance.runTransaction((transaction) async {
      transaction.update(Firestore.instance.collection('user').document(id), {'name': 'new name'});
    });

交易是否有(主要)不利因素?例如,它们是否更昂贵(Firebase计费,而不是计算)?毕竟,Firestore数据库上的数据可能会发生更改,这将导致最多5次重试.

Are there any (major) downsides to transactions? For example, are they more expensive (Firebase billing, not computationally)? After all there might be changes to the data on the Firestore database which will result in up to 5 retries.

供参考: https://firebase.google.com/docs/firestore/manage-data/transactions

您还可以使用事务对数据进行原子更改. 这对于增加总票数有点费劲,它是 正确的方法来进行更复杂的更改."

"You can also make atomic changes to data using transactions. While this is a bit heavy-handed for incrementing a vote total, it is the right approach for more complex changes."

https://codelabs.developers.google.com/codelabs/flutter-firebase/#10

推荐答案

使用您要显示的特定代码示例,使用事务几乎没有优势.如果您的文档更新对文档进行了静态更改,而不考虑其现有数据,则交易是没有意义的.您提议的事务实际上只是更新的较慢版本,因为它必须与服务器往返两次才能进行更改.普通更新仅使用一次往返.

With the specific code samples you're showing, there is little advantage to using a transaction. If your document update makes a static change to a document, without regard to its existing data, a transaction doesn't make sense. The transaction you're proposing is actually just a slower version of the update, since it has to round-trip with the server twice in order to make the change. A plain update just uses a single round trip.

例如,如果要将数据追加到字符串中,则两个客户端可能会覆盖彼此的更改,具体取决于它们何时读取文档.使用事务,您可以确保无论执行追加的时间如何,每个追加都将生效,因为面对并发时,将使用更新的数据重试该事务.

For example, if you want to append data to a string, two clients might overwrite each other's changes, depending on when they each read the document. Using a transaction, you can be sure that each append is going to take effect, no matter when the append was executed, since the transaction will be retried with updated data in the face of concurrency.

通常,如果可能的话,您应该努力完成没有交易的工作.例如,更喜欢在事务外使用FieldValue.increment(),而不是在事务内手动递增.

Typically, you should strive to get your work done without transactions if possible. For example, prefer to use FieldValue.increment() outside of a transaction instead of manually incrementing within a transaction.

事务用于在对文档(或通常为多个文档)进行更改的情况下使用,这些文档必须在最终写入之前将字段的当前值考虑在内.这样可以防止两个客户端在应该实际协同工作时破坏彼此的更改.

Transactions are intended to be used when you have changes to make to a document (or, typically, multiple documents) that must take the current values of its fields into account before making the final write. This prevents two clients from clobbering each others' changes when they should actually work in tandem.

请阅读有关文档中交易的更多信息以更好地理解他们如何工作.它不太像SQL事务.

Please read more about transactions in the documentation to better understand how they work. It is not quite like SQL transactions.

这篇关于在Google Firestore中使用交易的弊端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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