firebase / firebase-unity-sdk

The Firebase SDK for Unity
http://firebase.google.com
Apache License 2.0
234 stars 38 forks source link

[Bug] internal::IsInitialized() Error When LogEvent #1099

Closed ak1320 closed 3 months ago

ak1320 commented 3 months ago

Description

I think I installed Firebase SDK without any problems. Google-services.json is in the Asset folder. When I want to use a "LogEvent" in the game, I get this error without any details.

Autoconnected Player "Autoconnected Player" System.ApplicationException: internal::IsInitialized() at Firebase.Analytics.FirebaseAnalytics.LogEvent (System.String name, Firebase.Analytics.Parameter[] parameters) [0x00000] in <00000000000000000000000000000000>:0 at PUS.Start () [0x00000] in <00000000000000000000000000000000>:0

The start function is as follows:

try
        {
            FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                FirebaseApp app = FirebaseApp.DefaultInstance;

            });
            FirebaseApp.LogLevel = LogLevel.Debug;
            Parameter[] parameters = { new Parameter("Player", ZPlayerPrefs.GetString("username")) };
            FirebaseAnalytics.LogEvent("opened_mall", parameters);
        }catch(Exception e)
        {
            Debug.Log(e);
        }

Reproducing the issue

No response

Firebase Unity SDK Version

12.2.0

Unity editor version

2022.3.40f1

Installation Method

.unitypackage

Problematic Firebase Component(s)

All

Other Firebase Component(s) in use

Analytics

Additional SDKs you are using

No response

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

No response

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

Expand Podfile.lock snippet
```yml 👀 Replace this line with the contents of your Podfile.lock! ```
google-oss-bot commented 3 months ago

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

argzdev commented 3 months ago

Hey @ak1320, thanks for reaching out. CheckAndFixDependenciesAsync runs asynchronously so you're likely logging events before the check completes. You should log your events only after the dependency check completes.

try
{
            FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                FirebaseApp app = FirebaseApp.DefaultInstance;
                // move your log events here
                FirebaseApp.LogLevel = LogLevel.Debug;
                Parameter[] parameters = { new Parameter("Player", ZPlayerPrefs.GetString("username")) };
                FirebaseAnalytics.LogEvent("opened_mall", parameters);
            });
} 
catch(Exception e)
{
    Debug.Log(e);
}

That said, I believe this resolves your issue. I'll go ahead and close this thread. Thanks!

ak1320 commented 3 months ago

image

Unfortunately, this code attempt did not work either. image

ak1320 commented 3 months ago

@argzdev issue not solved yet, please open this issue again.

argzdev commented 3 months ago

I noticed you're using ContinueWith. The check has to be done on the main thread. You need to use ContinueWithOnMainThread.

e.g.

      FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
        ....
      });

You may also use our Firebase Unity quickstart for references.

This will likely resolve your issue. I'll go ahead and close this thread. Thanks!