0

When I run code to insert data in a Google Firestore database, the database gets updated, but the application just freezes as if database operation is not completed.

But when I add a 1000ms timeout in wait, then my code works properly.

    private  async Task AddData1(string project)
    {
        FirestoreDb db = FirestoreDb.Create(project);
        DocumentReference docRef = db.Collection("employees").Document("100250");
        Dictionary<string, object> user = new Dictionary<string, object>
        {
            {"Name","nakshatra"},
            {"Age","7"},
            {"Domicile","Himanchal"}
       };
        label1.Text += "Inserting data to firestore database";

      WriteResult x=  await docRef.SetAsync(user);

        Console.WriteLine(" Data added for employee no 100242");
        label1.Text += "Data Inserted successfully";

    }

and the function call is

AddData1("testproj1-7d81b").Wait(); //app freezes
AddData1("testproj1-7d81b").Wait(1000); // app works fine

After running the code, it should insert the data in a Firestore database. Then should print "Data Inserted successfully";

But instead the app freezes.

Super Jade
  • 5,609
  • 7
  • 39
  • 61

1 Answers1

1

Don't block on async code using Wait. The proper way to call asynchronous code is with await:

await AddData1("testproj1-7d81b");

And yes, this will cause async to "grow" through your codebase. This is perfectly natural and correct.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810