循环访问firestore查询结果

问题描述 投票:0回答:2

我有一个查询,它定义了我正在寻找的文件,如何循环它们并将它们添加到列表中?

查询和循环到目前为止:

// Create a reference to the cities collection
val facRef = firestoreInstance.collection("faculty")
// Create a query against the collection.
val query = facRef.whereEqualTo("university", list[position].ID)
// Cycle
listFac.clear()
for (document in query) {
    val fac = Faculty()
    fac.ID = document.id
    fac.Desc = document["Desc"].toString()
    listFac.add(fac)
}
android kotlin google-cloud-firestore
2个回答
1
投票

当您调用查询时,您需要给它时间来检索结果,例如,在快照方法中执行您想要执行的操作

for (document in query.snapshots) {
val fac = Faculty()
fac.ID = document.id
fac.Desc = document["Desc"].toString()
listFac.add(fac)

}


0
投票

查询不会立即包含数据库中的文档。您需要先告诉它检索这些文档,例如通过调用query.snapshots

for (document in query.snapshots) {
    val fac = Faculty()
    fac.ID = document.id
    fac.Desc = document["Desc"].toString()
    listFac.add(fac)
}

另见:

© www.soinside.com 2019 - 2024. All rights reserved.