火力基地检索元素 [英] Firebase & Retrieving Elements
问题描述
我正在尝试从Firebase读取x元素,但是我有一种误解……
I'm trying to read x amount of elements from Firebase, but I have a feeling I'm misunderstanding something...
DataSnapshot返回正确的子计数,但是当我尝试遍历这些子计数时,该循环永远不会执行.
DataSnapshot returns the correct child count, however when I try to loop through the children, the loop never executes.
注意:Kotlin中的代码
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)
}
})
}
推荐答案
这对我有用:
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
// This returns the correct child count...
println("count: "+snapshot.children.count().toString())
children.forEach {
println(it.toString())
}
}
})
}
输出:
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} }
更新
在评论中,有人讨论了为什么snapshot.children.count()
有效,而children.count()
无效.该问题是由两个事实引起的:
Update
In the comments there was some discussion about why snapshot.children.count()
works, while children.count()
doesn't. The problem is caused by two facts:
- Firebase的
DataSnapshot.getChildren()
返回一个Iterable
,它只能向前迭代(Iterable
的协定也是如此). - 科特林
count()
在Iterable
上循环计数.
- Firebase's
DataSnapshot.getChildren()
returns anIterable
, which can be iterated forward only (as is the contract of anIterable
). - The Kotlin
count()
loops over theIterable
to count its items.
因此,在完成Kotlin的count()
之后,Iterable
在序列的末尾.随后的for
循环不再循环.在我的代码段中,我分别调用snapshot.children
以获得单独的迭代器以获取计数.
So after Kotlin's count()
is done, the Iterable
is at the end of the sequence. The subsequent for
loop has nothing to loop over anymore. In my snippet I call snapshot.children
separately to get a separate iterator to get the count.
了解Kotlin如何实现count()
时,最好使用Firebase内置的childrenCount
:
Knowing how Kotlin implements count()
it is better to use Firebase's built-in childrenCount
:
println("count: "+snapshot.childrenCount)
这篇关于火力基地检索元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!