Search code examples
swiftuitableviewfirebaseseguegoogle-cloud-firestore

Passing a document reference to a second view controller


Can someone please show me how I can make it so that when I click on a specific row in a tableView, a document reference is passed to a second ViewController allowing me to show the fields inside the subcollection "Friends". At the moment I can do this but without using autoID. Please may someone how I can do this using an autoID? Any help would be greatly appreciated, many thanks!!

Current Firebase Console

What I would like - Firebase Console

First ViewController

    func loadData() {
    db.collection("Users").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                let data = document.data()
                let name = data["name"] as? String ?? ""
                let newName = UsersNames(name: name)
                self.nameArray.append(newName)
            }
            self.tableView.reloadData()
        }
    }
}

Second ViewController

    func loadData() {
    db.collection("Users").document("Hello").collection("Friends").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                let data = document.data()
                let name = data["name"] as? String ?? ""
                let details = data["details"] as? String ?? ""
                let newFriends = Friends(friendName: name, friendDetails: details)
                self.friendsArray.append(newFriends)
            }
            self.tableView.reloadData()
        }
    }
}

Solution

  • If you want to do it that way, firstly you need to add a documentID property to your UsersNames object:

        struct UsersNames {
    
            var documentID: String //<-- Add this
            var name: String
        }
    

    Then update your loadData() function in your First VC to get the documentID from each Firestore document and append in into your Array:

            for document in querySnapshot!.documents {
                let data = document.data()
                let documentID = document.documentID //<-- Add this
                let name = data["name"] as? String ?? ""
                let newName = UsersNames(documentID: documentID, name: name) //<-- Change this
                self.nameArray.append(newName)
            }
    

    In First VC, you want to perform a Segue to your Second VC when a cell is selected, and pass the documentID for the selected object in your Array to the Second VC.

        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            performSegue(withIdentifier: "SecondVC", sender: self)
        } 
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let indexPath = tableView.indexPathForSelectedRow {
                let destinationVC = segue.destination as! SecondVC
                let documentId = nameArray[indexPath.row].documentID
                destinationVC.documentID = documentID
            }
        }
    

    In your SecondVC create a property to receive the documentID:

        var documentID: String!
    

    In your SecondVC loadData() function, you can now access the documentID that was passed from your First VC:

        db.collection("Users").document(documentID).collection("Friends").getDocuments() { //..