【发布时间】:2016-06-24 14:02:48
【问题描述】:
我无法展示从数据库中获取的对象。 我声明了包含要展示的项目的数组:
var tournaments = [TournamentItem]()
我的结构的 TournamentItem 初始化函数:
init(snapshot: FIRDataSnapshot!) {
key = snapshot.key
ref = snapshot.ref
type = snapshot.childSnapshotForPath("type").value as! String
name = snapshot.childSnapshotForPath("name").value as! String
numberOfParticipants = snapshot.childSnapshotForPath("numberOfParticipants").value as! Int
tournamentMode = snapshot.childSnapshotForPath("mode").value as! Bool
completed = snapshot.childSnapshotForPath("completed").value as! Bool
}
获取数据的函数如下:
func getTournamentListSnapshot() {
ref.observeEventType(.Value, withBlock: { snapshot in
//print("\(snapshot.key) -> \(snapshot.value)")
for item in snapshot.children {
let tournamentItem = TournamentItem(snapshot: item as! FIRDataSnapshot)
self.tournaments.append(tournamentItem)
}
if self.tournaments.isEmpty { //not a subject of discussion
self.showAlertOfEmptyTournamentsArray()
}
})
}
我在我的 ViewDidLoad 方法中调用它。 问题是调用此方法后,我的数组为空,但 Firebase 数据库不是。 我试图调试这整个事情,但我找不到任何解决这个问题的方法。
我的 TableViewController 委托方法如下所示:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tournaments.count
}
【问题讨论】:
-
你在
if self.tournaments.isEmpty { //not a subject of discussion self.showAlertOfEmptyTournamentsArray() }之后忘记打电话给tableView.reloadData()了吗? -
在从 observeEventType 异步回调之后检查数组吗?
-
@Santosh 就是这种情况。现在可以正常使用了,谢谢!
-
太棒了!我已将其添加到答案中,接受它作为答案。
标签: ios swift firebase firebase-realtime-database