Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant display data after using .getDocuments in Firestore and trying to place the document IDs into an array and displaying them using a tableview

I have a class

class List {

var name: String

init(name: String) {

    self.name = name

}
}

which is used to make the array. I declared the array

var providersList = [List]()

then used the function

func downloadData() {

    db.collection("\(finalSelection.lowercased())_providers").getDocuments() { (querySnapshot, err) in
         if let err = err {
             print("Error getting documents: \(err)")
         } else {
             for document in querySnapshot!.documents {
                self.providersList.append(List(name: "\(document.documentID)"))


             }
         }
     }

 }

to download the data. The data downloads correctly because there is no error and I also used print() to confirm the data is correct. However, when I use the functions for tableview

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MoreCell", for: indexPath) as! MoreInfoTableViewCell

    cell.moreInfoLabel.text = providersList[indexPath.row].name

    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return providersList.count
}

the table view isn't populated. I know this method works because I have used something similar to it in another area of my code (not downloading the data but populating the table view)

Edit: I tried to remove the class and append to the array normally without using the .name from the class. However, this didn't do anything either

like image 728
equ1nox Avatar asked Dec 01 '25 03:12

equ1nox


1 Answers

Ok, so I found out the solution to this when I was laying in bed about to sleep (lol). In the function downloadData(), I have to put self.tableView.reloadData() after appending to the array. I will include this for anyone who faced a similar problem like me.

func downloadData() {

db.collection("\(finalSelection.lowercased())_providers").getDocuments() { (querySnapshot, err) in
     if let err = err {
         print("Error getting documents: \(err)")
     } else {
         for document in querySnapshot!.documents {
            self.providersList.append(List(name: "\(document.documentID)"))
            self.tableView.reloadData()


         }
     }
 }

}

like image 100
equ1nox Avatar answered Dec 02 '25 20:12

equ1nox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!