quickstart-unity
quickstart-unity copied to clipboard
Strange behavior occurs when sending requests at the same time
[REQUIRED] Please fill in the following fields:
- Unity editor version: 2022.1.5
- Firebase Unity SDK version: 9.1.0
- Problematic Firebase Component: Firestore
- Platform you are using the Unity editor on: Windows
- Platform you are targeting: Android
- Scripting Runtime: IL2CPP
[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
*/
}
});
This issue does not seem to follow the issue template. Make sure you provide all the required information.
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.
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();
}
});
}
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.
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?
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.
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.