Firestore交易被多次触发,导致数据错误 [英] Firestore transactions getting triggered multiple times resulting in wrong data

查看:53
本文介绍了Firestore交易被多次触发,导致数据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个云功能,每次喜欢/不喜欢交易时都会触发该功能.此功能增加/减少 likesCount .我已经使用了Firestore交易来实现相同的目的.我认为问题在于Transaction块中的代码被多次执行,根据文档,这可能是正确的.

So I have a cloud function that is triggered each time a transaction is liked/unliked. This function increments/decrements the likesCount. I've used firestore transactions to achieve the same. I think the problem is the Code inside the Transaction block is getting executed multiple times, which may be correct as per the documentation.

但是我的喜欢"计数在某些时间未正确更新.

But my Likes count are being updated incorrectly at certain times.

 return firestore.runTransaction(function (transaction) {
        return transaction.get(transRef).then(function (transDoc) {
            let currentLikesCount = transDoc.get("likesCount");
            if (event.data && !event.data.previous) {
                newLikesCount = currentLikesCount == 0 || isNaN(currentLikesCount) ? 1 : transDoc.get("likesCount") + 1;
            } else {
                newLikesCount = currentLikesCount == 0 || isNaN(currentLikesCount) ? 0 : transDoc.get("likesCount") - 1;
            }
            transaction.update(transRef, { likesCount: newLikesCount });
        });
    });

任何人都有类似的经历

推荐答案

人们终于找到了导致这种意外行为的原因.

Guys finally found out the cause for this unexpected behaviour.

如果您的应用程序将需要大量流量,Firestore不适合维护计数器.他们在文档中提到了它.他们建议的解决方案是使用分布式计数器.

Firestore isn't suitable for maintaining counters if your application is going to be traffic intensive. They have mentioned it in their documentation. The solution they suggest is to use a Distributed counter.

许多实时应用程序都有充当计数器的文档.例如,您可能会在帖子中计为赞",或在特定项目中计为收藏".

Many realtime apps have documents that act as counters. For example, you might count 'likes' on a post, or 'favorites' of a specific item.

在Cloud Firestore中,您只能大约更新一次文档每秒,对于某些高流量的应用来说可能太低了.

In Cloud Firestore, you can only update a single document about once per second, which might be too low for some high-traffic applications.

https://cloud.google.com/firestore/docs/solutions/counters

我不相信这种方法,因为它对于一个简单的用例来说太复杂了,那是我偶然发现以下博客的时候

I wasn't convinced with that approach as it's too complex for a simple use case, which is when I stumbled across the following blog

https://medium.com/evenbit/on-与Cloudfirestore碰撞过程7af26242bc2d

这些家伙结合使用了Firestore + Firebase,从而消除了他们的弱点.

These guys used a combination of Firestore + Firebase thereby eliminating their weaknesses.

Cloud Firestore位置便利,靠近Firebase Realtime数据库,并且两者易于使用,混合和匹配在应用程序中.您可以自由选择在两者中存储数据满足您需求的项目的地方.

Cloud Firestore is sitting conveniently close to the Firebase Realtime Database, and the two are easily available to use, mix and match within an application. You can freely choose to store data in both places for your project, if that serves your needs.

因此,为什么不使用实时数据库来发挥其优势之一:管理来自分布式客户端的快速数据流.哪一个尝试汇总和统计数据时出现的问题消防站.

So, why not use the Realtime database for one of its strengths: to manage fast data streams from distributed clients. Which is the one problem that arises when trying to aggregate and count data in the Firestore.

说Firestore是对Realtime数据库的升级(如其广告所示),但是具有不同目的的不同数据库,并且两者都可以并且应该共存于大型应用程序中,这是不正确的.那是我的想法.

Its not correct to say that Firestore is an upgrade to the Realtime database (as it is advertised) but a different database with different purposes and both can and should coexist in a large scale application. That's my thought.

这篇关于Firestore交易被多次触发,导致数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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