0

Currently i am doing it like this:

 DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("Leaders").Child("List1");
         reference.GetValueAsync().ContinueWith(task =>
         {
             int totalChildren = (int)task.Result.ChildrenCount;
               //Do more stuff
         } 

I thought, why get whole Snapshot to count how many children.

Any different way without fetching whole Snapshot?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Dror
  • 1,262
  • 3
  • 21
  • 34

1 Answers1

2

Firebase does not have a built-in count operator. So you'll either have to download all child nodes to count them (as you're doing now), or keep a separate property where you track the number of child nodes.

The latter takes more work when writing, but leads to much simpler read performance. You'll find that this read-vs-write performance is a common trade-off in NoSQL databases.

For more on this, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks, so much limits the more you get into it.I can't save locally the number of children as it is a leaderboard and can be updated from different users. Hope they add that count option. – Dror Dec 17 '18 at 16:20
  • It is extremely unlike that a count option will be added to the Firebase Realtime Database at this point. But note that plenty of developers before you were able to show a count based on what I've shown above. All these approaches work across multiple users. – Frank van Puffelen Dec 17 '18 at 17:04
  • Yeah, your links are a good sum up of different solutions! I liked the 'shallow=true' , can't find any document explaining of it can be used via Unity Firebase SDK, as it seems only to work with Rest calls. – Dror Dec 17 '18 at 19:07
  • That is correct, shallow loading is only in the REST API. Keeping a counter in the database is by far the best approach. – Frank van Puffelen Dec 17 '18 at 19:27