作者:杨barkema_252 | 来源:互联网 | 2023-05-18 06:40
Iamnewtoswift3,andIgotstucktothisproblem.Ihavetwofunctions,thefirstfunctiongets
I am new to swift 3, and I got stuck to this problem. I have two functions, the first function gets the value of a key in my FirebaseDatabase. The second function displays a the variable retrieved from the FirebaseDatase inside a TableView. The problem is that the second function launches before the first one. This makes the value return nil.
我是swift 3的新手,我遇到了这个问题。我有两个函数,第一个函数获取FirebaseDatabase中键的值。第二个函数显示从TableView内的FirebaseDatase检索的变量。问题是第二个函数在第一个函数之前启动。这使得值返回为零。
First function:
self.shopItems = [String]()
databaseRef.observe(FIRDataEventType.value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! FIRDataSnapshot
let dictiOnary= snap.value as! [String: AnyObject]
self.shopItems.append(dictionary["Name"] as! String)
print(self.shopItems)
}
})
The second function:
第二个功能:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: self.cellReuseIdentifier, for: indexPath) as UITableViewCell
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4), execute: {
cell.textLabel?.text = self.shopItems[indexPath.row]
})
return cell
}
I know i can use the Dispatch-Wait method, but I want the second function to wait until the first one is done. How would i do so?
我知道我可以使用Dispatch-Wait方法,但是我希望第二个函数等到第一个函数完成。我该怎么办?
3 个解决方案