将数据从Firebase附加到阵列 [英] Appending data from Firebase to array
本文介绍了将数据从Firebase附加到阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些要在tableView中显示的数组:
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
}
推荐答案
由于该调用是异步的,因此需要在数据返回的回调内重新加载表
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()
这篇关于将数据从Firebase附加到阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文