Home > Software design >  Unity Firebase RTDB doesn't work after building
Unity Firebase RTDB doesn't work after building

Time:06-13

Unity version 2020.3.22f1, Firebase SDK 9.0.0 dotnet4

I've imported both the analytics and real-time database SDK. The Analytics works perfectly fine.

Regarding the database, building an Android app bundle and uploading to internal test or building an APK and uploading directly to my phone or building for IOS and uploading to test-flight all 3 results with an error.

This is how I initialize Firebase-

FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
    //init analytics
    FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);

    DatabaseReference = FirebaseDatabase.DefaultInstance.RootReference;
    Debug.Log(JsonConvert.SerializeObject(DatabaseReference.Database.App.Options));
});

This is how I use the database (increment the win field of a specific level) -

DatabaseReference.Child("Levels").Child($"Level{levelNum}").Child("Wins").RunTransaction((mutableData) =>
{
    mutableData.Value = Int32.Parse(mutableData.Value.ToString())   1;
    return TransactionResult.Success(mutableData);
});

Referring to the log above in the DatabaseRefrence initialization, in the editor, I can see all the configuration properties - databaseUrl,apikey,AppId, etc...

Debugging the APK on my phone the Options property only includes the databaseUrl. And when trying to perform a transaction to the database an error is being thrown -

W/Unity: Exception in transaction delegate, aborting transaction
    System.NullReferenceException: Object reference not set to an instance of an object.
    at ....(Firebase.Database.MutableData mutableData)

Things I've tried so far-

  1. I've added the SHA1/SHA256 of both my debug Keystore and googles console App integrity
  2. I've checked and the XML files are being generated with all the details successfully at the streamingAssets folder and at Assets\Plugins\Android\FirebaseApp.androidlib\res\values\google-services.xml
  3. I've tried Initializing the Firebase app manually as mentioned here - https://stackoverflow.com/a/66874818/7210967, doing that indeed results with the debug.log above to include all the Options parameters but the same error occurs as if it doesn't actually use it. (I've tried doing that both with the configuration files in place and removed them completely).
  4. I've tried overriding the default app instance Options.

I've read some posts saying that Proguard obfuscation might cause errors with firebase? couldn't find anything related to Unity.

If anyone has any ideas, please share! ty

CodePudding user response:

Transactions in Firebase Realtime Database work a bit differently than you might expect, as they immediately invoke you handler with the client's guess about the current value of the node, which in general is going to be null.

So when you call mutableData.Value in your code, you get back null and you then call ToString() on it, which leads to the error you get. To solve this, first check whether the mutableData.Value is null before invoking methods on it.

int current = mutableData.Value is null ? 0 : Int32.Parse(mutableData.Value.ToString());
mutableData.Value = current   1;
return TransactionResult.Success(mutableData);

Syntax errors are possible in the above, as it's been a while since I wrote C#.


A transaction send both the SDKs guess and your new value based on that guess to the server, which then does a compare-and-set operation. If the guess doesn't match the actual value in the database, the server rejects the write with the current value, which the client then uses to call your transaction handler again with an updated current guess.

  • Related