0

I use this code

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

Because that path has many childs, I only retrieve 5 results each time with LimitToLast (and also EndAt later)

The problem is that it takes 4 seconds to retrieve a result, it used to be like 1 second. I'm worried that it's only getting worse. And even though I limit the task to only 5 it still apparently go through all the childs in the DB, my database is already sorted by key by default, what could I do to make it faster?


I tried Frank's solution and changed it to

DatabaseReference dtr =  FirebaseDatabase.DefaultInstance.GetReference(path);
        
var query = dtr.OrderByKey().EndAt(lastKey).LimitToLast(5);

query.KeepSynced(true);

query.GetValueAsync().ContinueWith....

But I still have to wait 4 seconds until task is returned

SHAI
  • 789
  • 3
  • 10
  • 43

1 Answers1

0

You're keeping the entire node synchronized by calling dtr.KeepSynced(true); on it. The fact that you then only retrieve 5 items from that synchronized node, means that the SDK still has to do the work for all the nodes - to then throw all but 5 of those nodes away.

If you only want to show the 5 nodes, call KeepSynced(true) on the query instead of its parent reference.

var query = dtr.OrderByKey().LimitToLast(5);
query.KeepSynced(true);
query.GetValueAsync()......
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I just tried that but it didn't change anything, I edited my post with your solution, can you tell me if I'm doing this right? thanks – SHAI Aug 05 '21 at 03:06
  • Be sure to uninstall and reinstall the app, to ensure it clears the (large) disk cache. – Frank van Puffelen Aug 05 '21 at 03:38
  • Still no change. I figured it only happens on the editor (Windows) but not on mobile. The app is super slow on Windows and it never happened before the DB became larger, I even removed KeepSynced. It's like it gets stuck after loading the task – SHAI Aug 16 '21 at 21:08
  • Ah, I must admit I've never tried this in the Unity desktop myself, so don't know if that would make a difference. This would be good information to add to your question, just in case somebody knows the problem/solution based on that. – Frank van Puffelen Aug 16 '21 at 21:36
  • Hey @frank maybe I do this wrong, but looking at your code- where is the 5 nodes limitation? Also, does it make sense to keep synchronize on 5 nodes? – SHAI Sep 22 '21 at 16:32
  • Oops... I forgot to copy/paste the `.LimitToLast(5)`, fixed that now. :) I'm not sure how to answer whether it makes sense to sync 5 nodes, but can say that it is technically possible. – Frank van Puffelen Sep 22 '21 at 16:51
  • I'm not sure it's the proper way, I move the discussion here-https://stackoverflow.com/questions/69288486/firebase-realtime-db-query-returns-an-old-datasnapshot-and-not-new-keys-unle – SHAI Sep 22 '21 at 17:07
  • After changing what you cache, be sure to delete and reinstall the app before measuring again - as I'm not certain how the cache will deal with the change. In general though, the best solution is to use a permanent listener and update the UI as the SDK gives you updated data. – Frank van Puffelen Sep 22 '21 at 17:41
  • But the solution doesn't make sense, because in order to see new values that were added to my DB, I need to listen to the entire DatabaseReference and not only to a query of 5 nodes – SHAI Sep 22 '21 at 17:44
  • I don't think that's the case, but I also don't see how it relates to my previous comment. If you attach a permanent listener to a query for the latest 5 nodes, you'll get (at most) two calls: first the information from the local cache, then the updated information from the server. – Frank van Puffelen Sep 22 '21 at 18:13
  • My comment was about KeepSynced. – SHAI Sep 22 '21 at 18:16
  • What do you mean by a permanent listener, what method do you use? – SHAI Sep 22 '21 at 18:16
  • Instead of using `GetValueAsync`, listen to one of the properties documented here: https://firebase.google.com/docs/database/unity/retrieve-data#listen_for_events (`ValueChanged` is the closest equivalent). – Frank van Puffelen Sep 22 '21 at 18:20
  • I have too many entries, sub entries and users (DB of car posts, DB for comments for a car post, DB for user's posts, DB for likes for a comment of a car post), it sounds too pricey to set listeners for everything. Some paths are not that important to be subscribed to, but I just want to retrieve updated values when entering there. Firebase makes it so hard for us. – SHAI Sep 22 '21 at 19:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237380/discussion-between-shai-and-frank-van-puffelen). – SHAI Sep 22 '21 at 19:44
  • I told you why you see the behavior you see, and how to get the behavior you need. I'm not sure what else I can do to help here. I highly recommend changing to a listener model though. No matter which database you use: if they offer an option for receiving realtime updates, that is likely to lead to the best user experience. – Frank van Puffelen Sep 22 '21 at 20:12
  • I can confirm now that KeepSynced(true) on the query itself prevents the slowness issue on windows. Thank you – SHAI Sep 30 '21 at 21:06