简体   繁体   中英

Getting same results with realtime database even when data has changed

I do a query on a path then add new data on the same path then read again with the same query and the new data is not in the result. I can see the new data in my FB console and if I restart my app, it will show. It's like I'm reading from cached data. What is wrong?

public static void GetScores(string readDbPath)
{
    FirebaseDatabase.DefaultInstance.GetReference(readDbPath).OrderByChild("score")
        .LimitToLast(Constants.FIREBASE_QUERY_ITEM_LIMIT)
      .GetValueAsync().ContinueWith(task =>
      {
          if (task.IsFaulted)
          {
              // Handle the error...
              Debug.LogError("FirebaseDatabase task.IsFaulted" + task.Exception.ToString());
          }
          else if (task.IsCompleted)
          {
              DataSnapshot snapshot = task.Result;
              // Do something with snapshot...
              List<Score> currentScoreList = new List<Score>();
              foreach (var rank in snapshot.Children)
              {
                  var highscoreobject = rank.Value as Dictionary<string, System.Object>;

                  string userID = highscoreobject["userID"].ToString();
                  int score = int.Parse(highscoreobject["score"].ToString());
                  currentScoreList.Add(new Score(score, userID));

              }
              OnStatsDataQueryReceived.Invoke(currentScoreList); // catched by leaderboard
          }
      });
}

It's very likely that you're using Firebase's disk persistence, which doesn't work well with Get calls. For a longer explanation of why that is, see my answer here: Firebase Offline Capabilities and addListenerForSingleValueEvent

So you'll have to choose: either use disk persistence, or use Get calls. The alternative to calling Get would be to monitor the ValueChanged event. In this case your callback will be invoked immediately when you change the value in the database. For more on this, see the Firebase documentation on listening for events .

This post was deleted stating it was just additional infos on my question. If fact it is the solution which is to use GoOffline() / GoOnline().

Thanks Frank for the detailed answer.

I tried with

FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(false)

but the problem stayed the same. Using listeners is not what I want since every time a player would send a score, every player on the same path would receive refreshed data so I'm worried about b/w costs and performance.

The best solution I just found is to call before using a get

FirebaseDatabase.DefaultInstance.GoOnline(); then right after I get a response I set

FirebaseDatabase.DefaultInstance.GoOffline(); So far no performance hit I can notice and I get what I want, fresh data on each get. Plus persistence if working off line then going back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM