skip to Main Content

I’m working on a Unity project where I use Firebase to manage user data. I am encountering a recurring issue where the asynchronous task GetValueAsync() does not complete, and the code within ContinueWithOnMainThread never executes.

Below I detail the context and what I have tried:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase;
using Firebase.Auth;
using Firebase.Database;
using Firebase.Extensions;
using System.Threading.Tasks;

public class InitFirebase : MonoBehaviour
{

    private void Awake()
    {
        StartCoroutine(InitializeFirebase());
    }

    private IEnumerator InitializeFirebase()
    {
        Firebase.FirebaseApp.LogLevel = Firebase.LogLevel.Debug;
        var dependencyCheck = FirebaseApp.CheckAndFixDependenciesAsync();
        yield return new WaitUntil(() => dependencyCheck.IsCompleted);

        if (dependencyCheck.Result == DependencyStatus.Available)
        {
            Debug.Log("Firebase initialized.");
           
            Debug.Log("Loadind data...");
            FirebaseDatabase.DefaultInstance.GetReference("users").GetValueAsync().ContinueWithOnMainThread(task => {
                 Debug.Log("This never runs");//this never runs
            });
        }
        else
        {
            Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyCheck.Result.ToString());
        }
    }


}

I also tried isolating the code, onButtonClick runs this after Firebase initialization is complete.

 Debug.Log("Loadind data...");
  FirebaseDatabase.DefaultInstance.GetReference("users").GetValueAsync().ContinueWithOnMainThread(task => {
     Debug.Log("This never runs");//this never runs
 });

I have my google-services.json, it seems to be creating firebase app succesfully, I can user FirebaseAuth, FirebaseAuth.DefaultInstance.CurrentUser; is not null, it also has the corresponding uid

I have internet connection, firebase rules are set to .write: true, .read: true. And im running the game in the Unity editor 2020.3.20f1 I have the latest firebase sdk 11.8.1 and it doesnt throw any type of error. Its like the task is never completing.

Problem:
The callback inside ContinueWithOnMainThread never executes, regardless of whether or not data is available at the specified reference. This occurs even after confirming that Firebase is correctly initialized and that there are no dependency issues (checked with CheckAndFixDependenciesAsync()).

Attempts to Resolve:

Checked Firebase Database security rules to ensure proper access.
Ensured that the internet connection is active when performing the operation.
Implemented detailed logs before and after the call to GetValueAsync() to confirm that the code reaches that point(it does not, ever.)

I also am using a Mac, and when i first runned the project it popped up this:

see here

I allowed anyways in Preferences->Securit & Privacy-> General.
It dind’t pop up no more.

2

Answers


  1. Make sure your task is completed successfully. Perhaps it is silently thrown an exception, and your ContinueWith... is not called. For example, if your task.Result is null.

    You can try to check it like this (writing here, so there can be errors):

    var task = FirebaseDatabase.DefaultInstance.GetReference("users").GetValueAsync();
    await task;
    
    // check `task.Result` here. 
    
    Login or Signup to reply.
  2. in addition to the earlier suggestions regarding checking task.Result etc, try initializing using CheckAndFixDependenciesAsync().ContinueWithOnMainThread( instead, similar to the following:

    Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
      var dependencyStatus = task.Result;
      if (dependencyStatus == Firebase.DependencyStatus.Available) {
        // Create and hold a reference to your FirebaseApp,
        // where app is a Firebase.FirebaseApp property of your application class.
           app = Firebase.FirebaseApp.DefaultInstance;
    
        // Set a flag here to indicate whether Firebase is ready to use by your app.
      } else {
        UnityEngine.Debug.LogError(System.String.Format(
          "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
        // Firebase Unity SDK is not safe to use here.
      }
    });
    

    This is the suggested method mentioned in "Add Firebase to your Unity project ".

    AFTER that, to debug null check each step of FirebaseDatabase.DefaultInstance.GetReference("users") and see if the reason why your debug never gets called is because its being called on a null object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search