【问题标题】:Transfer data using prepare for segue使用 prepare for segue 传输数据
【发布时间】: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


【解决方案1】:

db.collection() 做了一些异步工作,所以所有这些代码:

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()

应该在你设置 myOption 数组的 for 循环之前。这样,一旦数据库检索到所有数据并且您在 for 循环中进行了所有设置,一切都将被设置。

【讨论】:

    【解决方案2】:
    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()
    
                }
            }
        }
    }
    

    由于它是一个异步调用,您可能必须在回调中编写 selectedDishTableViewController 序列。

    建议:顺序委托方法应该简单,不应该处理所有这些数据库操作尝试优化它。

    【讨论】:

    • 这不起作用,因为在异步 db.collection()...getdocuments() 之前调用了第二个 tableview 的 viewdidload 关于如何解决这个问题的任何想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多