非常简单的Firestore事务失败 [英] Very simple Firestore transaction fails

查看:145
本文介绍了非常简单的Firestore事务失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力应对超级简单的交易。它总是失败并显示Transaction failed all retries消息,但除了 logcat 之外没有任何错误消息。

I'm struggling with a super simple transaction. It always fails with the message "Transaction failed all retries" but there are no error messages besides that on the logcat.

当我调试它时,我发现它正在重试几次。我真的不知道为什么,因为其他交易没有问题。

When I debug it, I see that it's being retried several times. I really don't know why, as other transactions run without issue.

我只想将一个文档从一个集合克隆到另一个集合中。从视频收藏夹(我知道这可以在@Alex积分的交易之外完成) out,但这只是失败的部分,真正的交易更长)

I just want to clone one document from one collection into another collection. Think from "videos" to "favorites" (I know this can be done outside of a transaction as @Alex points out, but this is just the failing part, the real transaction is longer)

private void copy(
    final DocumentReference SOURCEDOCREF, 
    final CollectionReference TARGETCOLREF) {

        Transaction.Function<? extends Void> transaction = new Transaction.Function<Void>() {

            @Nullable
            @Override
            public Void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {

                DocumentSnapshot doc = transaction.get(SOURCEDOCREF);
                if (doc.exists()) {
                    DocumentReference favoriteRef = TARGETCOLREF.document("FV_" + doc.getId());
                    Map<String, Object> data = doc.getData();
                    transaction.set(favoriteRef, data);
                    return null;

                    // NOTE: This is reached, ie. the source doc exists
                    // the data recovered, and set into the transaction.
                } else
                    throw new FirebaseFirestoreException("Item does not exist", FirebaseFirestoreException.Code.NOT_FOUND);
            }
        };

        setMode(MODE_SPLASH);
        FirebaseFirestore.getInstance().runTransaction(transaction)
                .addOnSuccessListener(
                        (Activity) getContext(),
                        new OnSuccessListener<Object>() {
                            @Override
                            public void onSuccess(Object aVoid) {
                                setMode(MODE_FOLLOW);
                            }
                        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        hide();
                        DialogHelper.customToast(getContext(), e.getMessage());
                    }
                });
    }


推荐答案

根据有关交易的文档

如果事务读取文档而另一个客户端修改
中的任何一个文档,Cloud Firestore将重试该事务。此功能
可确保事务在最新且一致的数据上运行。

If a transaction reads documents and another client modifies any of those documents, Cloud Firestore retries the transaction. This feature ensures that the transaction runs on up-to-date and consistent data.

因此,您可以预期如果在交易完成之前修改了源文档,则将重试您的交易。

So, you can expect that your transaction will be retried if the source document has been modified before the transaction complete.

您还可以预期交易可能会失败。

You can also expect a transaction can fail.


由于以下原因,事务可能会失败:

A transaction can fail for the following reasons:


  • 事务包含读取后的操作写操作。读取操作必须始终在任何写入操作之前。

  • 事务读取在事务之外修改的文档。在这种情况下,事务会自动再次运行。
    该事务被重试有限次。

失败的事务返回错误并且不向$写入任何内容b $ b数据库。您无需回滚交易; Cloud
Firestore会自动执行此操作。

A failed transaction returns an error and does not write anything to the database. You do not need to roll back the transaction; Cloud Firestore does this automatically.

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

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