【发布时间】:2016-03-26 12:23:18
【问题描述】:
我正在尝试从 Firebase 读取 x 数量的元素,但我感觉我误解了一些东西...
DataSnapshot 返回正确的子节点计数,但是当我尝试遍历子节点时,循环永远不会执行。
注意: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)
}
})
}
【问题讨论】: