firebase-unity-sdk icon indicating copy to clipboard operation
firebase-unity-sdk copied to clipboard

[Bug] Don't call Firebase functions before CheckDependencies has finished

Open WiseKodama opened this issue 1 year ago • 7 comments

Description

          Non-fatal Exception: java.lang.Exception: InvalidOperationException : Don't call Firebase functions before CheckDependencies has finished
       at Firebase.FirebaseApp.ThrowIfCheckDependenciesRunning(Firebase.FirebaseApp)
       at Firebase.FirebaseApp.GetInstance(Firebase.FirebaseApp)
       at Firebase.FirebaseApp.get_DefaultInstance(Firebase.FirebaseApp)
       at Firebase.Analytics.FirebaseAnalytics..cctor(Firebase.Analytics.FirebaseAnalytics.)

Why is this a thing? Why is accessing Static fields(FirebaseAnalytics.EventScreenView and FirebaseAnalytics.ParametersScreenName) an issue? We had an issue where RemoteConfig did not work if Firebase functions were called before CheckDependencies finished, however I have no info if this is happening to these users...

Please remove this exception and ensure that it works normally even if a FirebaseFunction is called.

Reproducing the issue

This is happening to our live users and reported through Crashlytics, so repocase is not known. I would assume a slow device should produce this...

Firebase Unity SDK Version

11.7.0

Unity editor version

2022.3.38f1

Installation Method

.unitypackage

Problematic Firebase Component(s)

Analytics

Other Firebase Component(s) in use

Crashlytics, Remote Config

Additional SDKs you are using

AppLovin MAX, Qonversion

Targeted Platform(s)

Android

Unity editor platform

Windows

Scripting Runtime

IL2CPP

Release Distribution Type

Pre-built SDK from https://firebase.google.com/download/unity

Relevant Log Output

Non-fatal Exception: java.lang.Exception: InvalidOperationException : Don't call Firebase functions before CheckDependencies has finished
       at Firebase.FirebaseApp.ThrowIfCheckDependenciesRunning(Firebase.FirebaseApp)
       at Firebase.FirebaseApp.GetInstance(Firebase.FirebaseApp)
       at Firebase.FirebaseApp.get_DefaultInstance(Firebase.FirebaseApp)
       at Firebase.Analytics.FirebaseAnalytics..cctor(Firebase.Analytics.FirebaseAnalytics.)

If using CocoaPods for Apple platforms, the project's Podfile.lock

Expand Podfile.lock snippet

👀 Replace this line with the contents of your Podfile.lock!

WiseKodama avatar Jul 22 '24 08:07 WiseKodama

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Jul 22 '24 08:07 google-oss-bot

Hi @WiseKodama, thanks for reaching out. The Firebase SDK checks to make sure that all the necessary dependencies are included before it runs.

Do you have the condition checker of CheckAndFixDependenciesAsync before running any Firebase calls?

protected virtual void Start() {
      FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
        dependencyStatus = task.Result;
        if (dependencyStatus == DependencyStatus.Available) {
          InitializeFirebase();
        } else {
          Debug.LogError(
            "Could not resolve all Firebase dependencies: " + dependencyStatus);
        }
      });
    }

You can take a look into our Firebase Unity Quickstart for reference. It'll help guide you in configuring your project correctly.

Let me know if that helps. Thanks!

argzdev avatar Jul 24 '24 09:07 argzdev

I do but more complex projects do not work that way. Sometimes you want to send analytics event at app start, currently I create a queue and send them off once Firebase dependency checker is done. However the issue is that to queue a specific event that uses for example FirebaseAnalytics.EventScreenView will call the constructor on FirebaseAnalytics resulting in the error above. Why doesn't firebase have a built in queuing system? It seems counter productive that static fields throw this error, should we really have a check infront of every static field accessor relating to Firebase?

WiseKodama avatar Jul 24 '24 10:07 WiseKodama

I understand and sorry for the issue you're experiencing right now. Unfortunately, the current design does not allow any calls prior to the CheckAndFixDependenciesAsync. I know this is not ideal, and this is something we're still trying to work on.

There were also previous discussions of this in the quickstart. I did see an alternative posted by one of the developers, however I haven't tried it myself.

For now, I'll mark this as a feature request, and bring this up to our engineers to see if we have updates or future plans. However, please note that we currently don't have a timeline for this. I'll reply back here once we receive feedback. Thanks!

argzdev avatar Jul 24 '24 10:07 argzdev

I fixed this as follows; For every time I need to send something to Firebase (e.g. an Event), I wrote a function and passed the eventcall in this function, like so:

  • First, write the function that accepts a string as the input:
 public static void CheckIfReady(string message)
 {

     Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
         Firebase.DependencyStatus dependencyStatus = task.Result;
         if (dependencyStatus == Firebase.DependencyStatus.Available)
         {
             Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;
             Debug.Log("Firebase is ready for use.");
             FirebaseAnalytics.LogEvent(message);
         }
         else
         {
             UnityEngine.Debug.LogError(System.String.Format(
               "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
         }
     });
 }
  • Then, you can call this function with the input event, like so:

CheckIfReady("Your LogEvent goes here...");

C'est Ça, no more errors.

KrazeyKami avatar Sep 21 '24 09:09 KrazeyKami

I also have a work around.

However I cannot believe Google would have such a lazy implementation, seeing as they have the highest talent supposedly.

The queueing of all actions should be on their end. Not to mention if you access Firebase before it’s done everything will miss fire.

Guess they don’t test or do code reviews.

Sadly there is no other good/free alternative so we are stuck patching the holes.

On Sat, 21 Sep 2024 at 11:20, Jeroen de Bonte @.***> wrote:

I fixed this as follows; For every time I need to send something to Firebase (e.g. an Event), I wrote a function and passed the eventcall in this function, like so:

  • First, write the function that accepts a string as the input:

public static void CheckIfReady(string message) {

 Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
     Firebase.DependencyStatus dependencyStatus = task.Result;
     if (dependencyStatus == Firebase.DependencyStatus.Available)
     {
         Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;
         Debug.Log("Firebase is ready for use.");
         FirebaseAnalytics.LogEvent(message);
     }
     else
     {
         UnityEngine.Debug.LogError(System.String.Format(
           "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
     }
 });

}

Then, you can call this function with the input event, like so:

CheckIfReady("Your LogEvent goes here...");

C'est ca, no more errors.

— Reply to this email directly, view it on GitHub https://github.com/firebase/firebase-unity-sdk/issues/1075#issuecomment-2365089501, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGC7FIMF2HV45QGXJMOTIQTZXU25LAVCNFSM6AAAAABLH2YGKOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNRVGA4DSNJQGE . You are receiving this because you were mentioned.Message ID: @.***>

WiseKodama avatar Sep 21 '24 09:09 WiseKodama

Issue still exists in the latest version of Firebase SDK.

rbyjoswald avatar May 27 '25 14:05 rbyjoswald