【问题标题】:Table view is not displaying the comments or username from Firebase表格视图不显示来自 Firebase 的评论或用户名
【发布时间】:2017-03-31 00:58:59
【问题描述】:

这是我用来从 firebase 获取用户的 cmets 和用户名以显示在 cmets 的 tableview 单元格中的代码。它目前不显示任何内容,但是当我发布 cmets 时,它会通过并且我可以在 firebase 数据库中看到它。我只需要阅读所有评论数据并显示出来。

func fetchComment()
{
    let ref = FIRDatabase.database().reference().child("Newsfeed").child(itemSelected.title)

    ref.observeSingleEvent(of: .value, with: { snapshot in
        if snapshot.hasChild("comments")
        {
            ref.child("comments").observeSingleEvent(of: .value, with: {snappy in
                for comPosted in snappy.children.allObjects as! [FIRDataSnapshot]
                {
                    let commentPosted = comment()

                    guard let comDict = comPosted.value as? [String: AnyObject]

                    else
                    {
                        continue
                    }

                    commentPosted.commentText = comDict["userComment"] as! String!
                    commentPosted.username = comDict["userId"] as! String!

                    self.comments.append(commentPosted)
                }
            })
        }
    })

    self.commentTableView.reloadData()
    ref.removeAllObservers()
}

【问题讨论】:

  • 从 firebase 获取数据后,您在哪里尝试重新加载表视图?
  • 哦,对不起,我在最后一行代码之前有 self.commentTableView.reloadData() 。但它仍然没有显示任何内容。
  • 您必须在for 循环之后调用reloadData,包括完成块。现在,您甚至早在检索数据之前就调用了reloadData
  • 谢谢,但不幸的是它仍然没有显示它:(
  • 您需要提供更多详细信息。展示您对reloadData 的更新使用以及相关的表格视图数据源方法。

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

Firebase 是异步的,因此代码需要让 Firebase 有时间从服务器返回数据。该数据仅在闭包内部有效,并且闭包外部的任何代码都将在内部代码之前执行。代码比互联网快得多!

因此,在 for 循环之后移动 tableView.reloadData 将确保它在填充 dataSource 数组后执行。

此外,代码中的两个观察事件是单个事件 - 这意味着它们不会保持连接到节点,因此 removeAllObservers 是无关的。

    let ref = FIRDatabase.database().reference().child("Newsfeed")
                         .child(itemSelected.title)

    ref.observeSingleEvent(of: .value, with: { snapshot in
        if snapshot.hasChild("comments")
        {
            ref.child("comments").observeSingleEvent(of: .value, with: {snappy in
                for comPosted in snappy.children.allObjects as! [FIRDataSnapshot]
                {
                    let commentPosted = comment()
                    let comDict = comPosted.value as! [String: AnyObject]
                    commentPosted.commentText = comDict["userComment"] as! String!
                    commentPosted.username = comDict["userId"] as! String!
                    self.comments.append(commentPosted)
                }
                self.commentTableView.reloadData()

                //A quick check to ensure the array is populated.
                //Can be removed.
                for c in self.comments {
                    print(c.commentText)
                    print(c.username)
                }  
            })
        }
    })

上面的代码已经过测试并且可以工作,所以如果 tableview 仍然没有显示数据,那么 tableView 委托函数可能存在问题。

【讨论】:

  • @SioneSevakiFangaiuiha 如果有帮助,别忘了接受答案!
猜你喜欢
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 2022-01-21
  • 2020-12-11
  • 1970-01-01
  • 2018-10-14
  • 2023-03-30
相关资源
最近更新 更多