question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Firestore] Asynchronous methods from library hang and don't return anything.

See original GitHub issue

Hello. I am trying to incorporate Cloud Firestore into a C# application. I created the async method below (and I call it from the main method of my application):

        public async Task firebaseStuff()
        {
            FirestoreDb db = FirestoreDb.Create(projectId);
            CollectionReference collection = db.Collection("users");
            QuerySnapshot allUsers = await collection.GetSnapshotAsync();
            MessageBox.Show("Done Querying!");
            return;
        }

The users collection only has one document. The GetSnapshotAsync() method doesn’t seem to return anything and the “Done Querying!” box never shows. Am I missing something?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
jskeetcommented, Mar 24, 2020

Right, the constructor itself can’t be asynchronous - but you can call a static factory method which is asynchronous, does all the async work, and then constructs an instance:

public static MainWindow CreateAsync()
{
    var doc = await CreateFirebaseDoc();
    return new MainWindow(doc);
}

I don’t know whether that will be the right solution for you or not, but it’s one option to consider.

What’s more important is that you understand why your previous code failed - by calling Wait(), you were saying “I’m not going to let the WPF dispatcher thread do anything else until the task returned by firebaseStuff has completed”. Unfortunately, the firebaseStuff method has said “When the task returned by AddAsync completes, I need to come back to the WPF Dispatcher thread in order to return and set the task result”. That’s why you’ve got a deadlock.

I’m going to close this issue now as it really isn’t related to Firestore or the client library - it’s just a matter of working correctly with async methods.

0reactions
nmggithubcommented, Mar 24, 2020

The app itself is a WPF app. I am calling the method from within the MainWindow.xaml.cs constructor which isn’t (and can’t be, to my knowledge) async. I just tried without .Wait() and it seems to have worked? I’m going to try and test it a few more ways before confirming.

EDIT: Yeah, it works. Thank you for explaining what I did wrong.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retry asynchronous functions | Cloud Functions for Firebase
This document describes how you can request asynchronous (non-HTTPS) background functions to retry on failure. Semantics of retry.
Read more >
angular - Async method does not return a value and is stuck
So if this.fireAuth.authState never completes, but emits values you can use the first operator to create a stream that completes after the first ......
Read more >
Troubleshoot Cloud Functions
Some Cloud Logging client libraries use an asynchronous process to write log entries. If a function crashes, or otherwise terminates, it is possible...
Read more >
Cloud Firestore
The collection method returns a CollectionReference class, which provides properties and methods to query and fetch the data from Cloud Firestore.
Read more >
Why are the Firebase API asynchronous?
It's easy to spot them because their API docs show that they don't directly return the data you want. Instead, they'll return things...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found