Firebase runTransaction 不起作用 - MutableData 为空 [英] Firebase runTransaction not working - MutableData is null

查看:26
本文介绍了Firebase runTransaction 不起作用 - MutableData 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Firebase 数据库的 runTransaction() 但它不起作用.这是我正在使用的代码.

I am trying to use runTransaction() of Firebase database but it is not working. Here is the code I am using.

numQuestionRef.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData mutableData) {
        String numQuestions = (String) mutableData.getValue();
        long value = Long.parseLong(numQuestions, 16);
        value++;
        String incHex = Long.toHexString(value);
        mutableData.setValue(incHex);
        return Transaction.success(mutableData);
    }

    @Override
    public void onComplete(FirebaseError firebaseError, boolean b, DataSnapshot dataSnapshot) {


    }
});

当我按下应用内的按钮时,此代码会被激活.当我启动应用程序后第一次按下按钮时,我的数据库没有改变.但是当我自启动应用程序后第二次按下按钮时,它会将其更新为下一个数字.我不明白这是什么问题,也不明白为什么它只在第二次按下按钮时才会出现.

This code is activated when I press a button within my app. When I press the button for the first time since launching the app, my database does not change. But when I press the button the second time since launching the app, it updates it to the next number. I don't understand what it wrong or why it only does it on the second button press.

推荐答案

您需要遵循 Firebase 处理交易的文档 并处理您的交易回调没有当前值的情况:

You'll want to follow the pattern used in the Firebase documentation for handling transactions and handle the case where there's no current value your transaction callback:

public Transaction.Result doTransaction(MutableData currentData) {
    long value = 0;
    if(currentData.getValue() != null) {
        String numQuestions = (String) currentData.getValue();
        value = Long.parseLong(numQuestions, 16);
    }
    value++;
    String incHex = Long.toHexString(value);
    currentData.setValue(incHex);
    return Transaction.success(currentData);
}

您需要这样做的原因是 Firebase 可能(并且经常会)多次执行您的事务回调,而您需要满足这一点.

The reason you need to do this is that Firebase may (and often will) execute your transaction callback multiple times and you need to cater for that.

  1. 当您第一次调用 runTransaction() 时,Firebase 客户端将立即调用您的 doTransaction() 回调及其对当前数据的当前猜测.通常这将是 null.
  2. 您的代码根据当前值返回下一个值.在上述情况下,如果当前值为 null,则新值将为 1.
  3. Firebase 客户端然后将假定的当前值和新值发送到服务器.
  4. 如果实际存储的值与假定的当前值相同,Firebase 服务器将写入您指定的新值.
  5. 如果实际存储的值与假定的当前值不同,Firebase 服务器将拒绝新值并将实际当前值发送给客户端.
  6. 在这个阶段,客户端返回到步骤 1,使用现在更新的假定当前值.
  1. When you first call runTransaction() the Firebase client will immediately invoke your doTransaction() callback with its current guess for the current data. Quite often this will be null.
  2. Your code returns the next value based on the current value. In the case above, if the current value was null the new value will be 1.
  3. The Firebase client then sends both the assumed current value and the new value to the server.
  4. If the actual stored value is the same as the assumed current value, the Firebase server writes the new value you specified.
  5. If the actual stored values is different from the assumed current value, the Firebase server rejects the new value and sends the actual current value to the client.
  6. At this stage the client goes back to step 1, with the now updated assumed current value.

如果这不能解释您所看到的行为,您可能需要检查传递给 onComplete() 的值.

If this does not explain the behavior you're seeing, you might want to check what values are passed into onComplete().

这篇关于Firebase runTransaction 不起作用 - MutableData 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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