Thursday, May 2, 2024
HomeGame Developmentc# - FirebaseReference.GetValueAsync() in Unity Not Completing

c# – FirebaseReference.GetValueAsync() in Unity Not Completing


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.

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

Further more I tried using an aync method and sorrounding the block with try/catch

    public async void OnAButtonClick(){
        try{
            var dbRef = FirebaseDatabase.DefaultInstance.GetReference("users");
            Debug.Log("Reference "+dbRef.ToString());
            Debug.Log("Getting db data");//this is what it last print
            
            var task = await dbRef.GetValueAsync();
            Debug.Log("Task completed");//it nevers gets here

        }
        catch(Exception e){
            //It also never gets to this point...
            Debug.Log("Error: ");
            Debug.Log(e);
        }
    } 

I have my google-services.json, it seems to be creating firebase app succesfully, I can use 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.

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

see here

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

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.)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments