Appending data from Firebase to array [duplicate]

怎甘沉沦 提交于 2021-01-29 11:05:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!