问题
I have these arrays that I want to display in a tableView:
private var firOrder = [String]()
private var firDate = [Int]()
I try to get and append the data like this:
override func viewDidLoad() {
ref = Database.database().reference() ref?.child("users").child(user).child("Orders").observe(.value, with: { (snapshot) in
let values = snapshot.value as? [String: AnyObject]
let order = values!["Order"] as! String
let date = values!["Date"] as! Int
print("order \(order)") //This prints correctly
print("date \(date)") //This prints correctly
self.firOrder.append(order)
self.firDate.append(date)
})
print(firOrder) //This prints empty and does not append to array.
print(firDate) //This prints empty and does not append to array
}
回答1:
Because the call is asynchronous , you need to reload the table inside the callback where the data returns
self.firOrder.append(order)
self.firDate.append(date)
self.tableView.reloadData()
来源:https://stackoverflow.com/questions/51601896/appending-data-from-firebase-to-array