【问题标题】:Getting document reference on Second view Controller在第二个视图控制器上获取文档参考
【发布时间】:2020-03-04 20:53:39
【问题描述】:

任何人都可以提供帮助,以便当用户单击客户名称(使用表格视图)时,他们的详细信息将出现在第二个视图控制器(VC)上。我认为这是因为我还没有将 documentID 拉到这里。我尝试将 documentID 添加到 Second VC .document(documentID)

初始 VC 代码

    var customerList = [CustomerLsModel]()
    override func viewDidLoad() {
            super.viewDidLoad()
            let db = Firestore.firestore()

            db.collection("customers").getDocuments() { (snapshot, error) in
                if let error = error{
                    print("Can't get customers: \(error)")
                    let alert = UIAlertController(title: "Error", message: "There was an issue in pulling your information. Please close the application and try again.", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
                    self.present(alert, animated: true)
                } else {
                    for document in snapshot!.documents{
                        let firstname = document["firstname"]
                        let lastname = document["lastname"]
                        let address = document["address"]
                        let customer = CustomerLsModel(firstname: firstname as! String?, lastname: lastname as! String?, address: address as! String?)

                        self.customerList.append(customer)
                        self.orderAndLimit()
                    }
                    self.customerTable.reloadData()
                }
            }
        }
    }

第二个风投

   let db = Firestore.firestore()
    db.collection("customers").document().getDocument { (document, Error) in
        if let document = document, document.exists{
            self.firstNameLabel.text = document.get("firstname") as? String
        }
    }

这是我的客户模型类

    class CustomerLsModel{
        var firstname: String!
        var lastname: String?
        var address: String?

        init(firstname: String?, lastname: String?, address: String?){
            self.firstname = firstname
            self.lastname = lastname
            self.address = address
        }

【问题讨论】:

  • 你的vc是怎么连接的?如果它们已经在内存中,您可以直接传递这些值,而无需在 firebase 再次请求它
  • public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let vc = storyboard?.instantiateViewController(withIdentifier: "CustomerRecord") as? CustomerRecordViewController self.navigationController?.pushViewController(vc!, animated: true) } 这就是它们的连接方式
  • 你为什么不通过 segue 传递值?
  • document("name of the document") 这里你必须包含文件的名称/id

标签: swift firebase uiviewcontroller google-cloud-firestore


【解决方案1】:

根据 Firebase documentation for Swift,如果您使用方法 document() 没有路径参数,则您正在为新文档创建 DocumentReference。所以当你在这个参考上尝试getDocument - 它是空的。

只有在参数中添加路径,才能从现有文档中获取数据。

希望对你有帮助!

【讨论】:

    【解决方案2】:

    答案实际上取决于您希望如何处理数据,但这里有两种方法。但您的直觉是正确的 - 您需要跟踪客户的 documentId。

    因此,您可以在从 Firestore 读取客户并传入所有字符串和 documentId 以填充每个对象时执行此操作

    class CustomerLsModel{
       var firstname: String!
       var lastname: String?
       var address: String?
       var docId: String!
    
       init(firstname: String?, lastname: String?, address: String?, docId: String) {
          self.firstname = firstname
          self.lastname = lastname
          self.address = address
          self.documentId = docId
       }
    }
    

    或者这个,通过传入 Firestore 快照使代码更简单

    class CustomerLsModel {
       var firstname: String?
       var lastname: String?
       var address: String?
       var docId: String!
    
        init(withDoc: QueryDocumentSnapshot) {
            self.docId = withDoc.documentID
            self.firstname = withDoc.get("first_name") as? String ?? "no first"
            self.lastname = withDoc.get("last_name") as? String ?? "no last"
            self.address = withDoc.get("addres") as? String ?? "no address"
        }
    }
    

    从那里,当用户选择客户时,您可以将该对象传递给详细视图控制器进行显示,或者只传递 docId 并在您的子视图控制器中使用它来从 Firestore 读取数据。

    大约有 100 种“传递”数据的方法 - Segue 是一种流行的选择,您也可以在细节视图控制器中设置一个“客户”变量,或者细节可以从主控制器读取数据。这真的取决于您的用例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多