-1

I retrieve 10 keys each time from my sorted DB.

FirebaseDatabase.DefaultInstance.GetReference("CarsForSale/")
   .OrderByKey().EndAt(lastSavedKey).LimitToLast(10).GetValueAsync()

It works fine. But when new items are added to the database, I can't retrieve those new values unless I reset the app. If I don't use the query .OrderByKey().EndAt().LimitToLast, I do get the new keys from the DB without resetting the app

My guess is when I query the value at a location, it locally caches it. My solution for a while was using KeepSynced(true) and it worked.

DatabaseReference dtr = FirebaseDatabase.DefaultInstance.GetReference(path);
dtr.KeepSynced(true);
dtr.OrderByKey().EndAt(lastKey).LimitToLast(10).GetValueAsync()

But it made my app very slow because that syncs and caches the entire DB reference. So instead I tried to apply KeepSynced only on the query of every sorted 10 keys but it doesn't make sense.

What can I do to make sure when I query keys from the DB to get the latest keys without having to restart my app or make it slow with caching?

SHAI
  • 789
  • 3
  • 10
  • 43

1 Answers1

0

But when new items are added to the database, I can't retrieve those new values unless I reset the app.

This is because the GetValueAsync() first gets the values from the cache, and only then updates the cache. It's an annoying behavior that unfortunately can't be changed, and that I explained here: Firebase Offline Capabilities and addListenerForSingleValueEvent. I also just updated that answer for the newer get/getData methods on Android/iOS, but I don't think that exists in the C#/Unity SDKs yet.

The options I explained in the linked answer apply here too, so I'm going to close your question as a duplicate. If you have additional question, just comment here (if it's about your code/C#) or there (if it's about the general caching behavior).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks. So I really don't know what to do now. How can I get updated values? it's a basic question. When does the cache get updated? KeepSynced- doesn't work properly as I explained here- https://stackoverflow.com/q/68659086/3430889 – SHAI Sep 22 '21 at 17:36
  • If the problem is as you've described there, let's keep the discussion going there too. – Frank van Puffelen Sep 22 '21 at 17:38