简体   繁体   中英

Firebase RTDB Unity - RunTransaction Always Returns Null

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

Getting an initial null for the current value is the expected behavior.

Firebase immediately invokes your callback with its best guess at the current value of the node, which is usually null . Even when you know that this cannot ever be the value, the SDK can't know that. So your code should handle the null (for example by returning an initial value like 0 ), and will then be called again once the SDK has gotten the updated current value from the server.

Also see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM