I capture the data from Firestore and assign to array, someone in other question told me maybe I need a completion handler but I don't understand how to work, it would be very helpful if you help me with the code and explain this concept to me.
class PetsTVC: UITableViewController {
var db: Firestore!
let uid = Auth.auth().currentUser?.uid
var petslist = [String]()
var pid = ""
var pets = [paw]()
override func viewDidLoad() {
super.viewDidLoad()
self.loadPets()
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
func loadPets(){
let photo1 = UIImage(named: "Log")
db = Firestore.firestore()
db.collection("users").document(uid!).getDocument { documentSnapshot, error in
guard let document = documentSnapshot else {
return
}
self.petslist = document["petslist"] as? Array ?? [""]
}
for pet in self.petslist {
self.db.collection("pets").document(pet).getDocument { documentSnapshot, error in
guard let document = documentSnapshot else {
return
}
guard let data = document.data() else {
return
}
let pid = data["pid"] as! String? ?? ""
let petName = data["petName"] as! String? ?? ""
let infoPet = paw(id: pid, petName: petName, imagenMascota:photo1)
self.pets.insert(infoPet, at: 0)
}
}
}
}
If you need anything else from the code just tell me, because I'm also new here and I don't know what to put in the questions.