问题
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