Firebase RTDB Unity – RunTransaction Always Returns Null

Issue

This Content is from Stack Overflow. Question asked by FlynnM

Initial call is:

    db_reference
    .Child("total_users")
    .RunTransaction(RunTransaction)
    .ContinueWithOnMainThread(task => {
        if (task.Exception != null) {
            Debug.Log("Transaction exception.");

        } else if (task.IsCompleted) {
            Debug.Log("Transaction complete.");
        }
    });

Which calls:

TransactionResult RunTransaction(MutableData mutableData) {
    List<object> data = mutableData.Value as List<object>;
    if (data != null) {
        Debug.Log("data not null: " + data);
    } else {
        Debug.Log("data null");
    }
    return TransactionResult.Success(mutableData);
}

Aim is to simply get a value from the mutable data and increment it, however this always returns null and never the data stored in RTDB. Transaction also completes with error other than logging null.

  • Firebase Unity SDK 9.4.0
  • Unity Version: 2021.3.9f1

In addition when I add in:

        Dictionary<string, object> newScoreMap = new Dictionary<string, object>();
        newScoreMap["total_users"] = 1;
        mutableData.Value = newScoreMap;

and return TransactionResult.Success(mutableData);

this does update in RTDB successfully.

Any help much appreciated.



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?