Firebase runTransaction not working - MutableData is null

Viewed 9223

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.

1 Answers
Related