0

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

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. I had the tableView.reloadData() in viewDidAppear I get the error: "Thread 1: Fatal error: Index out of range" when trying to populate the tableView with: firOrder[indexPath.row]
which count you supply in numberOfRows ?? , also do you use both arrays as the dataSource of the table ??
Ahh! Well spotted!! I used the wrong count in numberOfRows Thanks a lot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.