Firebase Firestore事务“所有重试都失败了" [英] Firebase Firestore transaction “Transaction failed all retries”

查看:75
本文介绍了Firebase Firestore事务“所有重试都失败了"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要获取文件时,所有的firestore交易都失败.

All of my firestore transactions fail when I want to get document.

我尝试让其他文件将规则更改为公开.我发现,当我使用if检查时,好像get函数返回了数据.

I've tried getting other files changed rules to be public. I've found out that when i use if checking it seems like get function returned data.

val currentUserDocument = firebaseFirestore.collection("user").document(firebaseAuth.currentUser!!.uid)
val classMemberDocument = firebaseFirestore.collection("class").document(remoteClassID).collection("member").document(firebaseAuth.currentUser!!.uid)

        firebaseFirestore.runTransaction { transaction ->
            val userSnapshot = transaction.get(currentUserDocument)

            val isInClass = userSnapshot.getBoolean("haveRemoteClass")!!
            val classID = userSnapshot.getString("remoteClassID")!!

            if (isInClass == true && classID == remoteClassID) {
                transaction.update(currentUserDocument, "haveRemoteClass", false)
                transaction.update(currentUserDocument, "remoteClassID", "")

                transaction.delete(classMemberDocument)
            } else {
                throw FirebaseFirestoreException("You aren't in this class!", FirebaseFirestoreException.Code.ABORTED)
            }

            null
        }

推荐答案

这通常意味着您在事务中使用的数据存在很多争用.

This typically means that the data that you're using in the transaction is seeing a lot of contention.

每次运行事务时,Firebase都会确定您在事务中使用的所有文档的当前状态,并将该状态和这些文档的新状态发送到服务器.如果在事务开始与服务器获取之间更改了您获得的文档,它将拒绝该事务,并且客户端重试.

Each time you run a transaction, Firebase determines the current state of all documents you use in the transaction, and sends that state and the new state of those documents to the server. If the documents that you got were changed between when the transaction started and when the server gets it, it rejects the transaction and the client retries.

要使客户端这样失败,它必须重试的次数超出了合理范围.考虑缩小交易范围以减少文件数量,或者寻找另一种减少竞争的方法(例如针对分布式计数器).

For the client to fail like this, it has to retry more often than is reasonable. Consider reducing the scope of your transaction to cover fewer documents, or find another way to reduce contention (such as the approach outlined for distributed counters).

这篇关于Firebase Firestore事务“所有重试都失败了"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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