【发布时间】:2018-08-01 17:32:06
【问题描述】:
我有一个视图控制器,它列出了 firestore 数据库中的数据。在 firestore 集合中,我有一堆文档,其中包含列表中显示的信息,还有一个名为 order 的文档,其中包含一个字段,该字段是我希望它们显示的顺序的字符串数组。我的代码抓住了这个:
self.db.collection("officers").document(school).collection(grade).document("order").getDocument {(document, error) in
if let document = document, document.exists {
self.officerNames = (document.data()!["order"] as! Array<String>)
然后应该使用数组order (officerNames) 中的字符串来查询同一集合中的文档(所有文档都有不同的role,因此它只会在快照中获取一个文档)并以与order (officerNames) 中的一组相同的顺序显示它们。
for item in 1...self.officerNames.count {
self.db.collection("officers").document(school).collection(grade).whereField("role", isEqualTo: self.officerNames[item-1]).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let officerMessage = document.data()["agenda"] as! String
let officerInfo = document.data()["short"] as! String
(a bunch of code here using that ^ ^ and due to the color I need item to be an integer)
}
}
}
}
我知道,如果我尝试在 self.collection("officers")..... 之前打印 item,数字会按 1 计数,但如果我在 for document in querySnapshot..... 中这样做,它们都会出现故障,这意味着某些文档的加载速度比其他文档快。我已经阅读了 Swift 中的异步函数(尽管我确实在 JavaScript 中使用了这些函数),但我真的很困惑如何使用它们,希望有一种更简单的方法可以做到这一点。在再次遍历循环之前,我可以等待以确保先前的文档已被加载和分析吗?
【问题讨论】:
标签: swift firebase google-cloud-firestore