I'm trying to read x amount of elements from Firebase, but I have a feeling I'm misunderstanding something...
DataSnapshot returns the correct child count, however when I try to loop through the children, the loop never executes.
Note: Code in Kotlin
fun list(count: Int, callback: ListCallback) {
val playersRef = firebase.child("players")
val queryRef = playersRef.orderByChild("rank").limitToFirst(count)
queryRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(error: FirebaseError?) {
Log.e("firebase", error!!.message)
}
override fun onDataChange(snapshot: DataSnapshot?) {
val children = snapshot!!.children
// This returns the correct child count...
Log.i("firebase", children.count().toString())
val list = ArrayList<Entry>()
// However, this loop never executes
children.forEach {
val e = Entry()
e.name = it.child("name").value as String
e.rank = it.child("rank").value as Long
e.wins = it.child("wins").value as Long
e.losses = it.child("losses").value as Long
Log.i("firebase", e.toString())
list.add(e)
}
callback.onList(list)
}
})
}