Most efficient way to count children with Firebase Database

烂漫一生 提交于 2020-05-28 09:37:05

问题


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?


回答1:


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:

  • In Firebase, is there a way to get the number of children of a node without loading all the node data?
  • Firebase count num children of parent
  • How to get size of an element/list in Firebase without get it all?


来源:https://stackoverflow.com/questions/53815822/most-efficient-way-to-count-children-with-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!