skip to Main Content

I am using the Unity SDK for Firebase Realtime Database. Most of the time it works perfectly. However, sometimes it returns old data for queries. Sometimes its just the one right before last update, sometimes, I’m not even sure how many updates in between its been.

You can see my DB reference below, note the levelDepth, keyCount, keysAquiredLevels, gold, currentLevelCompleted values.

Below, is an image of the raw response I get in Unity when using var response = await databaseReference.GetValueAsync(); You’ll notice all those properties are different from a previous update.

This code works most of the time, I really have to hammer it 10-20 times to reproduce this issue, however it happens! and I am not sure why.

I’ve set FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(false); and I attempted to set keepSync to both true and false, but the issue still happens.

void Start()
{
    FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(false);
    Firebase.AppOptions options = AppOptions.LoadFromJsonConfig(firebaseOptions.text);
    FirebaseApp.Create(options);
}

public static async Task<CurrentLevelConfig> GetCurrentLevelConfig()
{
    DatabaseReference databaseReference = FirebaseDatabase.DefaultInstance
                                                                    .RootReference
                                                                    .Child("users")
                                                                    .Child(FirebaseConfig.user.UserId)
                                                                    .Child("currentLevelConfig");

    var response = await databaseReference.GetValueAsync();
    if (response.Exists)
    {
        CurrentLevelConfig config = JsonConvert.DeserializeObject<CurrentLevelConfig>(response.GetRawJsonValue());
        Debug.Log("Got current level config!");
        return config;
    }

    return null;
}

Any thoughts would be greatly appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    @joe-spiro's comment for adding CheckAndFixDependenciesAsync seemed to fix the issue! I will keep testing but as of now I am no longer to reproduce, marking this as answered.


  2. Try running the same query but instead of awaiting the result pass a callback using ContinueWithOnMainThread as shown in Retrieving Data – Read Data Once

    To elaborate, are you running this query on a thread you are spawning? It’s possible this may be a race condition.

    UPDATE with the prospective solution from my subsequent comment:

    Call FirebaseApp.CheckAndFixDependenciesAsync before running all Firebase functionality. Use a callback and then run FirebaseApp.Create.

    An example of doing this using the callback method and DefaultInstance (which calls Create() under the hood) can be seen in Add Firebase to your Unity Project.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search