简体   繁体   English

在第二个视图控制器上获取文档参考

[英]Getting document reference on Second view Controller

Would anyone be able to help so that when a user clicks on a customer name (using a table view) that their details will appear on the second view controller(VC).任何人都可以提供帮助,以便当用户单击客户名称(使用表视图)时,他们的详细信息将显示在第二个视图控制器(VC)上。 I think it is because I haven't pulled the documentID through to this.我认为这是因为我还没有将 documentID 拉到这里。 I've tried adding in documentID into Second VC .document(documentID)我试过将 documentID 添加到 Second VC .document(documentID)

Initial VC code初始 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()
                }
            }
        }
    }

Second VC第二个VC

   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
        }
    }

This is my customer model class这是我的客户模型类

    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
        }

According to Firebase documentation for Swift if you use method document() without path parameter you are creating DocumentReference to new document.根据 Swift 的 Firebase 文档,如果您使用不带路径参数的方法document() ,则您正在为新文档创建DocumentReference So when you try getDocument on this reference - it's empty.所以当你在这个引用上尝试getDocument时 - 它是空的。

Only if you add path in parameter you will get data from existing document.只有在参数中添加路径,您才会从现有文档中获取数据。

I hope it will help!我希望它会有所帮助!

The answer will really depend on how you want to work with the data but here's two ways.答案实际上取决于您希望如何处理数据,但有两种方法。 But your instinct is correct - you'll need to keep track of the customers documentId.但是您的直觉是正确的 - 您需要跟踪客户的 documentId。

So you could do this when reading the customers from Firestore and pass in all of the strings and documentId to populate each object因此,您可以在从 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
   }
}

or this, which makes the code a bit simpler by passing in the Firestore snapshot或者这个,通过传入 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"
    }
}

from there, when a user selects a customer, you could either pass that object to the detail view controller for display or, just pass the docId and use that in your child viewController to read the data from Firestore.从那里,当用户选择客户时,您可以将该对象传递给详细视图控制器进行显示,或者只传递 docId 并在您的子视图控制器中使用它从 Firestore 读取数据。

There are about 100 ways to 'pass' data - a Segue is a popular choice you could also set a 'customer' var in the detail viewController or the detail could read the data from the master.大约有 100 种方法可以“传递”数据——Segue 是一种流行的选择,你也可以在细节视图控制器中设置一个“客户”变量,或者细节可以从主节点读取数据。 It really depends on your use case.这实际上取决于您的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM