Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

800
Visualizações
Unity firestore - How to run GetSnapshotAsync() right after CheckAndFixDependenciesAsync()?

using Unity, I am trying to intialize my the firestore using CheckAndFixDependenciesAsync() and getting a document from firestore using GetSnapshotAsync(). From a firebase article, it is possible to add a continuation for the task with ContinueWithOnMainThread as shown in the example here:

Debug.Log("Checking Dependencies");
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(fixTask =>
{
    Assert.IsNull(fixTask.Exception);
    Debug.Log("Authenticating");
    var auth = FirebaseAuth.DefaultInstance;
    auth.SignInAnonymouslyAsync().ContinueWithOnMainThread(authTask =>
    {
        Assert.IsNull(authTask.Exception);
        Debug.Log("Signed in!");
        var successes = PlayerPrefs.GetInt("Successes", 0);
        PlayerPrefs.SetInt("Successes", ++successes);
        Debug.Log($"Successes: {successes}");
        auth.SignOut();
        Debug.Log("Signed Out");
    });
});

However, this is not the case for my project, where I want to use GetSnapshotAsync() instead to retrieve some 'level' data - everything inside the block is not running.

 private void Start()
    {
        Debug.Log("Database awake ");
        //check if all dependency is in project
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
        {
            string lvCode = "oHFxRAIpIHfT8moiYSl9";
            Debug.Log("Get level :" + lvCode);

            DocumentReference levelref = db.Collection("level").Document(lvCode);

            levelref.GetSnapshotAsync().ContinueWithOnMainThread(querySnapshotTask =>
            {
                //Debug.Log here is not ran
                Debug.Log("Completed? " + querySnapshotTask.Result);
                Debug.Log("Faulted? " + querySnapshotTask.IsFaulted);

            });
        });
}

Can someone explain why is this happening to my code and possibly provide some alternatives that use the ContinueWith methods? (as opposed to await and coroutine solutions)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Author of that article here 😃

I can't quite see if you're doing anything wrong, but my guess is that maybe you're doing a db = FirebaseFirestore.DefaultInstance outside of that block (ignore this if you're doing a lazy iniitalization). Calling that before CheckAndFixDependenciesAsync could be causing your issue (CheckAndFixDependenciesAsync verifies that DefaultInstance can be called without erroring out).

Subbing out your db reference might fix it:

private void Start()
{
    Debug.Log("Database awake ");
    //check if all dependency is in project
    FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
    {
        string lvCode = "oHFxRAIpIHfT8moiYSl9";
        Debug.Log("Get level :" + lvCode);

        DocumentReference levelref = FirebaseFirestore.DefaultInstance.Collection("level").Document(lvCode);

        levelref.GetSnapshotAsync().ContinueWithOnMainThread(querySnapshotTask =>
        {
            //Debug.Log here is not ran
            Debug.Log("Completed? " + querySnapshotTask.Result);
            Debug.Log("Faulted? " + querySnapshotTask.IsFaulted);

        });
    });
}

Also, pay close attention to any error logging, changing the first ContinueWith to ContinueWithOnMainThread might reveal more errors (some versions of Unity do fail to report errors in background tasks).

Finally it's worth checking that you actually do have Firestore setup with your current game. Assuming you have a google-services.json or GoogleService-Info.plist in your Assets/ directory, you can go to "Window>Firebase>Documentation": Menu open to "Window>Firebase>Documentation"

And click "Open in Console" to jump right to your project and double check that Firestore is setup: "Documentation" page open with "Open in Console" visible

If you do find that changing the first ContinueWith to ContinueWithOnMainThread fixes your issue completely, it could be worth filing a bug. Generally the Firebase libraries should be thread safe, and since Firestore is in beta maybe it's a use case that was missed.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda