Firebase runTransaction无法正常工作 [英] Firebase runTransaction not working

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

问题描述

我正在尝试为一个Firebase数据库使用runTransaction,但它不起作用。这里是我使用的代码。

$ p $ numQuestionRef.runTransaction(new Transaction.Handler(){
@Override
public Transaction.Result doTransaction(MutableData mutableData){
String numQuestions =(String)mutableData.getValue(); $ b $ long value = Long.parseLong(numQuestions,16);
value ++ ;
字符串incHex = Long.toHexString(value);
mutableData.setValue(incHex);
返回Transaction.success(mutableData);
}

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

$ b}
});

当我在应用程序中按下按钮时,此代码被激活。自从启动应用程序后第一次按下按钮时,我的数据库不会改变。但是,当我从启动应用程序第二次按下按钮,它将更新到下一个数字。我不明白是什么错,或为什么它只是按下第二个按钮。

解决方案

你会想请遵循 Firebase文档中用于处理交易的格式

a>,并处理事务回调没有当前值的情况:

$ 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);
返回Transaction.success(currentData);



$ b

您需要这样做的原因是,Firebase可能(也常常会)


  1. 当您第一次调用 runTransaction() Firebase客户端会立即使用当前猜测的当前数据调用您的 doTransaction()回调。通常这会是 null

  2. 您的代码基于当前值返回下一个值。在上面的例子中,如果当前值是 null ,新值将是 1

  3. 然后,Firebase客户端会将假定的当前值和新值发送到服务器。
  4. 如果实际存储值与假定的当前值相同,则Firebase服务器写入您指定的新值。
  5. 如果实际存储的值与假定的当前值不同,则Firebase服务器将拒绝新值,并将实际当前值发送给客户端。 / li>


  6. $ b
    $ b
  7. 在此阶段,客户端返回到步骤1,如果这不能解释你所看到的行为,你可能想要检查传入 onComplete()的值是什么。


    I am trying to use runTransaction for a 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);
    }
    

    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. 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.

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

    这篇关于Firebase runTransaction无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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