- Home /
FirebaseAuth.DefaultInstance competes with another instance -> only one works
Hello everyone,
I am observing a strange behaviour. Right after the start of my programm my FirebaseManagerAuth initializes the connection to firebase
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.
Can you help my to find an elegant way to solve this problem? I could probably put everything together in one class but as mentioned that is excatly not what I want.
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());
}
Answer by XoetziX · Oct 29, 2021 at 05:59 PM
The answer can be found here: https://stackoverflow.com/questions/69719567/firebaseauth-defaultinstance-competes-with-another-instance-only-one-works