Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

799
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!