firebase / quickstart-unity

Firebase Quickstart Samples for Unity
https://firebase.google.com/games
Apache License 2.0
828 stars 428 forks source link

Strange behavior occurs when sending requests at the same time #1292

Closed towberapp closed 2 years ago

towberapp commented 2 years ago

[REQUIRED] Please fill in the following fields:

[REQUIRED] Please describe the issue here:

If you send 2 requests at the same time: 1 for writing and 1 for reading, then when reading you will receive what was sent for writing, and not what is stored in the database.

If you send 1 read request, then get all the data from the Firestore as usual.

In the Unity editor, concurrent requests are handled correctly. BUT when compiling for Android, what happens is what is indicated.

Relevant Code:

DocumentReference docRef = db.Collection("users").Document("aturing");

//write 1
Dictionary<string, object> userWriteFirst = new Dictionary<string, object>
{
        { "First", "Alan" },
};
docRef.SetAsync(userWriteFirst).ContinueWithOnMainThread(task => {
        Debug.Log("Added data to the aturing document in the users collection.");
});

//read1
docRef.GetSnapshotAsync().ContinueWithOnMainThread(task =>
{
  DocumentSnapshot snapshot = task.Result;
  if (snapshot.Exists) {

    Dictionary<string, object> user = snapshot.ToDictionary();
    foreach (KeyValuePair<string, object> pair in city) {

         Debug.Log(String.Format("{0}: {1}", pair.Key, pair.Value));

        /* 
           At this point, you expect to see the entire array of data retrieved from the Firebase database, 
           but you only get this:
            > First: Alan
            Even if there are dozens of rows in the database
         */
    }
});
google-oss-bot commented 2 years ago

This issue does not seem to follow the issue template. Make sure you provide all the required information.

dconeybe commented 2 years ago

Drive-by comment: This looks very similar to https://github.com/firebase/firebase-js-sdk/issues/5895.

@towberapp Could you let us know if you have Firestore persistence enabled or disabled? If you haven't explicitly enabled or disabled it, then it's enabled.

towberapp commented 2 years ago

Persistence is disable.

My init firebase code:

    private void InitFirebase() 
        {            
            Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
                var dependencyStatus = task.Result;

                if (dependencyStatus == Firebase.DependencyStatus.Available)
                {                    
                    if (FirebaseFirestore.DefaultInstance.Settings.PersistenceEnabled)
                        FirebaseFirestore.DefaultInstance.Settings.PersistenceEnabled = false;                  

                      this.isFirebaseInit = true;

                      if (Application.internetReachability != NetworkReachability.NotReachable)
                          AfterInitFirebase();
                }
            });
        }
towberapp commented 2 years ago

I test whith FirebaseFirestore.DefaultInstance.Settings.PersistenceEnabled = true And I got the same result when compiling for Android: when I request data, I get only those that I just sent, and not those that are in the database.

ehsannas commented 2 years ago

According to the API reference, SetAsync's default behavior is to overwrite the document content - which seems to be what you're doing first, so it's conceivable that the GetSnapshotAsync method could return what the first one wrote. Did you intend to use UpdateAsync instead? or am I missing something?

towberapp commented 2 years ago

This is probably how it works. If there is a record and reading at the same time, then when reading, it takes from the cache in the first place.

wu-hui commented 2 years ago

Yeah, that is right. The default behavior for GetSnapshotAsync will include what you just wrote (even if the backend has not updated it's document with that mutation yet). This is to facilitate quicker response time many client Apps need.

Closing it as it is working as intended.