Home > Software design >  FirebaseAuth.DefaultInstance competes with another instance -> only one works
FirebaseAuth.DefaultInstance competes with another instance -> only one works

Time:10-30

I am using Firebase with Unity and I am observing a strange behaviour. Right after the start of my programm my FirebaseManagerAuth initializes the connection to firebase authentication:

 FirebaseAuth fbAuth = FirebaseAuth.DefaultInstance;

and in parallel my FirebaseManagerGame initializes the connection to the firebase database:

 DatabaseReference DBreference = FirebaseDatabase.DefaultInstance.RootReference;

I separated both in 2 classes assinged to 2 different game objects in order to keep each class smaller and easier to maintain.

Both Firebase actions seem to compete with each other because it is always only one of both which succeeds to get its reference. The other yields a "Object reference not set to an instance of an object". It seems to be some kind of racing condition because the "winner" changes from time to time.

If I deactivate one game object the initilization of the other always succeeds.


I have problems to find out what the actual reason for this problem ist. The stacktrace does not provide any (for me) useful information.

That's the debug output:

Gotcha - AUTH! Object reference not set to an instance of an object UnityEngine.Debug:Log (object) FirebaseManagerAuth:InitializeFirebase () (at Assets/Scripts/FirebaseManagerAuth.cs:68) FirebaseManagerAuth:b__5_0 (System.Threading.Tasks.Task`1<Firebase.DependencyStatus>) (at Assets/Scripts/FirebaseManagerAuth.cs:32) System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()

I would really appreciate if you could help me to solve this issue!


Here is the relevant code:

FirebaseManagerAuth

 public class FirebaseManagerAuth : MonoBehaviour
 {
     public static FirebaseManagerAuth instance;
     [SerializeField] private PlayerDataSO playerData;
 
     //Firebase variables
     [Header("Firebase")]
     public DependencyStatus dependencyStatus;
     public FirebaseAuth fbAuth;
     public FirebaseUser fbUser;
 
 
     void Awake()
     {
         //Check that all of the necessary dependencies for Firebase are present on the system
         FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
         {
             dependencyStatus = task.Result;
             if (dependencyStatus == DependencyStatus.Available)
             {
                 //If they are avalible Initialize Firebase
                 InitializeFirebase();
             }
             else
             {
                 Debug.LogError("Could not resolve all Firebase dependencies: "   dependencyStatus);
             }
         });
 
         if (instance == null)
         {
             instance = this;
             Debug.Log("FirebaseManagerAuth - awake - instance was null");
         }
         else if (instance != null)
         {
             Debug.Log("Instance already exists, destroying object!");
             Destroy(this);
         }
     }
     private void InitializeFirebase()
     {
         try
         {
             fbAuth = FirebaseAuth.DefaultInstance;
         }
         catch (Exception e)
         {
             Debug.Log("Gotcha - AUTH! "   e.Message);
             //throw e;
         }
         if (fbAuth == null)
             Debug.Log("INIT FirebaseManagerAUTH   fbAuth = null");
     }

FirebaseManagerGame (Database)

 public class FirebaseManagerGame : MonoBehaviour
 {
     public static FirebaseManagerGame instance;
     [SerializeField] private PlayerDataSO playerData;
 
     //Firebase variables
     [Header("Firebase")]
     public DependencyStatus dependencyStatus;
     public DatabaseReference DBreference;
        
 
     void Awake()
     {
   // excatly the same as above. 
     }
 
     private void InitializeFirebase()
     {
         try
         {
             DBreference = FirebaseDatabase.DefaultInstance.RootReference;
         }
         catch (Exception e)
         {
             Debug.Log("Gotcha - GAME! "   e.Message);
             throw e;
         }
         Debug.Log("INIT FirebaseManagerGAME   DBreference: "   DBreference.ToString());
     }

CodePudding user response:

not an expert but I'm using Unity & Firebase in a current project so hopefully the following suggestions help a bit.

First - Try changing your CheckAndFixDependenciesAsync() call to look like the following. Put it in an async method and just await the Check. This is todo with calling the method on the main thread correctly. Unity doesn't handle a 'ContinueWith' call very nicely. There's a particular article about firebase and async in Unity issues by google..

async void Awake()
{
    var dependencyStatus = await FirebaseApp.CheckAndFixDependenciesAsync();
    if (dependencyStatus == DependencyStatus.Available)
    {
        // all good, firebase init passed
        Debug.Log("Firebase Loaded");
        InitializeFirebase();
    }
    else
    {
        Debug.Log("Firebase Failed");
    }
}

That might be enough to fix the issues you're having. Second - As far as my understanding goes you only need to perform the Check once in the project at startup. So you could remove it from the two classes above and just enable them once the check has passed. e.g.

// This class is enabled at startup, and does the firebase check
// before you enable any other firebase objects in the scene.
public class FirebaseCheck : MonoBehaviour
{
[Header("Firebase Objects")]
public GameObject FirebaseManagerAuth;
public GameObject FirebaseManagerGame;

async void Awake()
{
    var dependencyStatus = await FirebaseApp.CheckAndFixDependenciesAsync();
    if (dependencyStatus == DependencyStatus.Available)
    {
        // all good, enable our gameobjects
        // they need not perform the check and can just Initialize
        // as 'instances' 
        FirebaseManagerAuth.SetActive(true);
        FirebaseManagerGame.SetActive(true);
    }
    else
    {
        Debug.Log("Firebase Failed");
    }
}

}

  • Related