This works for me:
val firebase: Firebase = Firebase("https://stackoverflow.firebaseio.com/34378547")
fun main(args: Array<String>) {
list(3)
Thread.sleep(5000)
}
fun list(count: Int) {
val playersRef = firebase.child("players")
val queryRef = playersRef.orderByChild("rank").limitToFirst(count)
queryRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(error: FirebaseError?) {
println(error!!.message)
}
override fun onDataChange(snapshot: DataSnapshot?) {
val children = snapshot!!.children
println("count: "+snapshot.children.count().toString())
children.forEach {
println(it.toString())
}
}
})
}
Exit:
count: 2
DataSnapshot { key = -K6-Zs5P1FJLk4zSgNZn, value = {wins=13, name=fluxi, rank=1, losses=1} }
DataSnapshot { key = -K6-ZtdotHkkBzs5on9X, value = {wins=10, name=puf, rank=2, losses=42} }
Update
The comments discussed why it works snapshot.children.count(), but children.count()not. The problem is caused by two facts:
- Firebase
DataSnapshot.getChildren()returns Iterable, which can only be renamed forward (like a contract Iterable). - Kotlin
count() Iterable .
, Kotlin count(), Iterable . for . snapshot.children , , .
, Kotlin count(), Firebase childrenCount:
println("count: "+snapshot.childrenCount)