Firebase runTransaction无法正常工作-MutableData为null [英] Firebase runTransaction not working - MutableData is null

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

问题描述

我正在尝试使用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.

推荐答案

您将要遵循

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为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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