ios - Firebase query is not populating array for table view. How can I resolve this issue?
问题描述
I have written a firebase query that gets information from my Competition db and then loads it into my distanceData (which is of type distanceModel, a class that I created) before populating the table view. However, the table view is not being populated.
Photo of Firebase db
Photo of distanceModel class
I tried putting print statements in the query to see if the data is being retrieved correctly, but nothing is being printed so the problem is most likely with the query.
var distanceData = [distanceModel]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate=self
tableView.dataSource=self
self.tableView.rowHeight = 200
refUser = Database.database().reference().child("userInfo");
let userID = Auth.auth().currentUser!.uid
let query = refUser?.queryOrdered(byChild: "userId").queryEqual(toValue: "\(userID)")
query?.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let childSnap = child as! DataSnapshot
var dict = childSnap.value as! [String: Any]
let level=dict["level"] as! String
if (level=="Beginner") {
self.refCompetition = Database.database().reference().child("competition1");
}
else if (level=="Intermediate") {
self.refCompetition = Database.database().reference().child("competition2");
}
else {
self.refCompetition = Database.database().reference().child("competition3");
}
}
})
retrieve the posts and listen for changes
refCompetition?.observe(DataEventType.value, with: { (snapshot) in
for users in snapshot.children.allObjects as! [DataSnapshot] {
let userObject = users.value as? [String: AnyObject]
let userName = userObject?["name"] as! String?
let userDistance = userObject?["distance"] as! String?
print(userName)
print(userDistance)
let dist = distanceModel(name: userName,distance: userDistance)
self.distanceData.append(dist)
}
})
self.tableView.reloadData()
}
Number of Rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return distanceData.count;
}
Contents for each cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DistanceCell", for: indexPath) as! DistanceCell
let dist: distanceModel
dist = distanceData[indexPath.row]
cell.nameLabel.text = dist.name
cell.distanceLabel.text = dist.distance
cell.rankNumber.text = String (indexPath.row+1)
return cell
}
It should get the data from the appropriate competition db and then render the results onto the table view.
解决方案
Try moving your self.tableView.reloadData() call into the Observer like below
refCompetition?.observe(DataEventType.value, with: { (snapshot) in
for users in snapshot.children.allObjects as! [DataSnapshot] {
let userObject = users.value as? [String: AnyObject]
let userName = userObject?["name"] as! String?
let userDistance = userObject?["distance"] as! String?
print(userName)
print(userDistance)
let dist = distanceModel(name: userName,distance: userDistance)
self.distanceData.append(dist)
}
self.tableView.reloadData()
})
}
推荐阅读
- sql - 带有标识字段的 Sybase INSERT INTO?
- python - 将动画绘制到 Jupyter Notebook:为什么在内部范围(即在函数中)调用 HTML(animator.to_html()) 时不起作用?
- javascript - 在我的应用程序上运行“ng serve”时“找不到模块'@angular/compiler-cli/ngcc'”
- java - Angular向java发送错误的日期格式并且无法在表中创建对象
- jquery - 调整颜色框覆盖以适应 div 而不是屏幕宽度
- go - 如何测试 http.Redirect 使用正确的重定向 URI?
- python - 如何从使用 scipy.interpolate.Rbf 创建的样条曲线中计算任意值?
- laravel - 控制器中的更改未反映显示先前的返回值
- python-3.x - 类中的文本 - href 语句
- spring-boot - 来自 Docker 的环境变量不会传递给 Spring