0

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
          }
      });
}
  • Maybe it **is** cached? Maybe this helps: [Clear firebase persistence after logout](https://stackoverflow.com/questions/38281761/clear-firebase-persistence-after-logout) ? – derHugo Dec 24 '19 at 11:36
  • How are you viewing results? The viewer many not get repainted. For example a DGV to view new results you have to set output to null and then back to the source : datagridview1.DataSource = null; datagridview1.DataSource = dt; – jdweng Dec 24 '19 at 11:38
  • @derHugo it is cached indeed but I need to clear the cache before the user wants to view the leaderboards not after logout – André Levasseur Dec 24 '19 at 22:59
  • @jdweng results are viewed in a Unity made UI. It is a mobile app. It is not mentioned explicitly but I put a Unity tag when asking the question – André Levasseur Dec 24 '19 at 23:03

2 Answers2

0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

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.