在Cloud Firestore(Firebase)中为孩子们祈祷



我正在尝试从实时数据库迁移到firebase中的Cloud firestore。我在重写代码时遇到了一些问题,这些代码在数据库中对子级进行迭代。

我的旧代码如下:

if (dataSnapshot.exists()) {
for (x in dataSnapshot.children) {
FetchInformation(x.key)
}
}

对于Firestore来说,它看起来是这样的:

if (snapshot!!.exists()) {
for (x in snapshot){
FetchInformation(x.key)
}

} else {
Log.d(TAG, "Current data: null")
}

然而,firestore不存在类似snapshot.children的东西,我收到了编译错误

'For-loop范围必须有一个'迭代器(('方法

您可以尝试

for(i in 0..snapshot) {
// x..y is the range [x, y]
}

for(i in 0 until snapshot) {
// x until y is the range [x, y>
}

参考

最新更新