How to check if Firebase Query Returns more than 1 Record?

Multi tool use


How to check if Firebase Query Returns more than 1 Record?
It seems firebase Query doesn't have methods for returned results count. but, I need to perform some data operation based on returned results.
var queryRef=database.child("Family").orderByChild("age").limitToFirst(2)
queryRef.addListenerForSingleValueEvent(object : ValueEventListener
{
override fun onDataChange(p0: DataSnapshot) {
if(p0.exists()){
// do something
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
but, similarly, I want p0.resultsCount instead of po.exists(). Help, me If any way it is possible to figure out returned results count.
onDataChange(p0: DataSnapshot) { if (DataSnapshot.exists() && DataSnapshot.getChildrenCount() > 1) }
1 Answer
1
You can count the number of children to which a Firebase Reference is pointing using the below method.
getChildrenCount()
For you it would be p0.getChildrenCount()
p0.getChildrenCount()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
in
onDataChange(p0: DataSnapshot) { if (DataSnapshot.exists() && DataSnapshot.getChildrenCount() > 1) }
– Vadim Eksler
4 mins ago