正在使用firebase数据填充表视图(数组未在.childAdded内更新)



我知道这篇文章的标题令人困惑,但我希望能在这里澄清。

我的最终目标:

基本上,我想使用从firebase实时数据库获得的数据来填充表视图。

我的问题

通过大量的故障排除,我相信我的问题是,当我在.childAdded闭包中向modelObjectsArray添加一些东西时,它似乎并没有真正更新数组。

我的代码

视图控制器

import Foundation
import UIKit
import Firebase
class CategoryDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
static let cellReuseIdentifier = "ratingCell"

var modelObjectArray: [Rating] = []

override func viewDidLoad() {
super.viewDidLoad()
retrieveData()
print(modelObjectArray) // prints [] for some reason...
//        print(modelObjectArray.count) // prints 0     
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

tableView.delegate = self
tableView.dataSource = self

//        modelObjectArray.append(Rating(score: 3, comment: "hahahaha, I really do enjoy beans...", timestamp: "Just now", likes: 10)) -> temporary to see if the tableview works (which it does)
tableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return modelObjectArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell: CategoryDetailTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "ratingCell") as! CategoryDetailTableViewCell

let modelObject = self.modelObjectArray[indexPath.row]
cell.commentLabel.text = modelObject.comment
cell.timestampLabel.text = String(modelObject.timestamp) + " hours ago"
cell.ratingLabel.text = String(modelObject.score) + "/10"

return cell
}

func retrieveData(){
let databaseRef: DatabaseReference = Database.database().reference()
databaseRef.child("uid").child("categories").child("looks").child("ratings").observe(.childAdded, with: { snapshot in
if let value = snapshot.value as? [String:Any] {
let rating = Rating(score: value["score"] as? Int ?? 0, comment: value["comment"] as? String ?? "", timestamp: value["creationDate"] as? String ?? "Just now", likes: value["likes"] as? Int ?? 0)
self.modelObjectArray.append(rating)
print(self.modelObjectArray.count) // prints 2
}
})
self.tableView.reloadData()
}
}

评级模型

class Rating {
var score: Int
var comment: String
var timestamp: String
var likes: Int

init(score: Int, comment: String, timestamp: String, likes: Int) {
self.score = score
self.comment = comment
self.timestamp = timestamp
self.likes = likes
}
}

我的Firebase

https://i.stack.imgur.com/uBhZt.jpg

总体

我觉得我遗漏了太多信息,所以如果你还需要,就告诉我吧。(也希望我在清理帖子时没有删除任何重要的代码——只是告诉我是否可能删除了(

不管怎样,提前感谢任何回答的人!

在异步块内重新加载数据。。否则,在从firebase 获取数据之前执行重新加载数据

func retrieveData(){
let databaseRef: DatabaseReference = Database.database().reference()
databaseRef.child("uid").child("categories").child("looks").child("ratings").observe(.childAdded, with: { snapshot in
if let value = snapshot.value as? [String:Any] {
let rating = Rating(score: value["score"] as? Int ?? 0, comment: value["comment"] as? String ?? "", timestamp: value["creationDate"] as? String ?? "Just now", likes: value["likes"] as? Int ?? 0)
self.modelObjectArray.append(rating)
print(self.modelObjectArray.count) // prints 2
self.tableView.reloadData()

}
})

}

第二种方法是使用完成处理程序

func retrieveData(completion:@escaping (Bool)->Void) {
let databaseRef: DatabaseReference = Database.database().reference()
databaseRef.child("uid").child("categories").child("looks").child("ratings").observe(.childAdded, with: { snapshot in
if let value = snapshot.value as? [String:Any] {
let rating = Rating(score: value["score"] as? Int ?? 0, comment: value["comment"] as? String ?? "", timestamp: value["creationDate"] as? String ?? "Just now", likes: value["likes"] as? Int ?? 0)
self.modelObjectArray.append(rating)
print(self.modelObjectArray.count) // prints 2
completion(true)

}else {
completion(false)
}
})

} 

像这样使用

override func viewDidLoad() {
super.viewDidLoad()
retrieveData { [weak self] (isSuccess) in
self?.tableViewe.reloadData()
}

// Do any additional setup after loading the view.
}

最新更新