Is there a way to get the number of node children without loading all node data in Android?

My data structure (userdetails)

-KCV32vWQECRlMvlgkGO Name: "asdf" Phoneid: "1zlkflakfhkf0e8" Phoneno: "9478567899" -KCV3s-lwv5i-VvFBaxq Name: "asas" Phoneid: "1c584jbascjasc8" Phoneno: "9999999999" 

My method

 queryRef.addChildEventListener(new ChildEventListener() { public void onCancelled(FirebaseError arg0) { // TODO Auto-generated method stub } public void onChildAdded(DataSnapshot arg0, String arg1) { // TODO Auto-generated method stub System.out.println("Size "+arg0.getChildrenCount()); } 

How to get score 2? I get 3 times two, because every time he goes inside and gets the child separately.

+5
source share
1 answer

The problem is that the snapshot that the child_added event gives is the new child data. So getChildrenCount() counts how many properties (children) a new child has (and has 3).

To do what you want, you must add a ValueEventListener event as follows:

 queryRef.addValueEventListener(new ValueEventListener() { public void onCancelled(FirebaseError arg0) { } public void onDataChanged(DataSnapshot arg0, String arg1) { System.out.println("Size "+arg0.getChildrenCount()); } }); 

This should return all children in the queryRef place every time the value changes (for example, a new child).

Tell me if this works, as I cannot try it myself.

+4
source

Source: https://habr.com/ru/post/1244866/


All Articles