【发布时间】:2018-05-08 12:14:51
【问题描述】:
我是 swift 和 firebase 服务的新手, 我使用火存储数据库作为我的数据库,我有一个第一个表视图,它读取所有数据并将其放入一个漂亮的表视图中。我的表格视图中的每个文档都有一个子集合。当用户按下一行时,我希望它打开带有子集合的第二个表格视图。
这是我为 segue 代码做的准备:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let indexPath = tableViewDishes.indexPathForSelectedRow {
db.collection("Restaurants").document("Applebees").collection("Menu").document(sections[indexPath.section].sectionName!).collection("Dishes").document(sections[indexPath.section].listofDishes![indexPath.row].DishName).collection("Options").getDocuments { (querySnapshot, error) in
if error != nil {print(error)}
else {
for document in querySnapshot!.documents {
//adding all the data to an array called myOption
}
}
}
let selectedDishTableViewController = segue.destination as! SelectedDishViewController
selectedDishTableViewController.myOption = self.myOption
selectedDishTableViewController.dish = self.sections[indexPath.section].listofDishes?[indexPath.row]
selectedDishTableViewController.sectionName = sections[indexPath.section].sectionName!
self.myOption.removeAll()
}
}
问题是,一旦我的代码到达 db.collection 行,当 myOption 为空数组时,它会立即跳转到 for 循环之后,然后它才会返回并将对象附加到我的数组中。
这导致我第一次按下一行时会得到一个空的第二个表格视图,当我返回并再次按下它时,我会得到所需的信息。
【问题讨论】:
-
我猜你的所有
db.collection(...)行都在进行异步调用。它真的进入了闭包(你写了for document in querySnapshot!.documents)之后。仅在完成查询后调用performSegue()。
标签: ios swift firebase google-cloud-firestore