-1

I do a firebase query to get a list of ids. In the next step I want to do another query for each id to get the name of the id and show it in a tableView Cell.

I try to store them into an array. But everytime I print the array its empty.

func loadDataFromFirebase() {
    databaseRef.child("user/...").observe(.value) { (snapshot) in
        self.keys.removeAll()
        if let snapshotValue = snapshot.value as? [String: [String: Any]] {
            for (key, value) in snapshotValue {
                if let like = value["like"], like as! String == "true" {
                    self.keys.append(key)
                }
            }
        }
        
        self.keys.forEach { variableNew in
            var connect = moreInfoWithPlaceId()
            var namesArray: [String] = []
            connect.fetchPoi(placeId: variableNew) {
                
                if let info = moreInfoWithPlaceId.infoWithPlaceId.first(where: { $0.place_id == variableNew }) {
                    namesArray.append(info.name)
                       }
                
                if let info = moreInfoWithPlaceId.infoWithPlaceId.first(where: { $0.place_id == variableNew }) {
                    namesArray.append(info.name)

                       }
            }
            
            print(namesArray)
            
        }
        
        self.tableView.reloadData()
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
submariner
  • 308
  • 1
  • 5
  • 23
  • 1
    Your print statement is executed before the response from the asynchronous call has been returned. It's so weird that you handle this correctly in [this question](https://stackoverflow.com/questions/75958754/firebase-swift-limiting-a-read-to-recent-entries) – Joakim Danielson Aug 12 '23 at 12:04

0 Answers0