表格视图不显示来自Firebase的评论或用户名 [英] Table view is not displaying the comments or username from Firebase

查看:133
本文介绍了表格视图不显示来自Firebase的评论或用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用来从firebase中获取用户的评论和用户名的代码,以便在我的tableview单元格中显示评论。它目前不显示任何内容,但是当我发布评论时,我可以在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()
$ b $ guard让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时间从服务器返回数据。该数据仅在封闭内有效封闭外的任何代码将在内部代码之前执行。代码比互联网快得多!

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



另外,代码中的两个观察事件是Single事件 - 意味着它们不保留附加到节点,所以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:{snappy in $ b $ for comPosted in snappy.children.allObjects as![FIRDataSnapshot]
{
let commentPosted = comment()
let comDict = comPosted.value as![String:AnyObject]
commentPosted .commentText = comDict [userComment] as!串!
commentPosted.username = comDict [userId] as!串!
self.comments.append(commentPosted)
}
self.commentTableView.reloadData()

//快速检查以确保数组已填充。
//可以删除。
for self.comments {
print(c.commentText)
print(c.username)
}
})
} $ b $上面的代码已经过测试,所以如果tableview仍然不显示数据,在tableView委托函数中可能有问题。


Here's the code I use to fetch the comments and username of users from firebase to display in my tableview cell for comments. It currently does not display anything but when I post comments it goes through and I can see it on the firebase database. I just need to read all the comment datas and display it.

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 is asynchronous and therefore the code needs to allow Firebase time to return the data from the server. That data is only valid inside the closure and any code outside the closure will execute before the code inside. Code is much faster than the internet!

So moving the tableView.reloadData right after the for loop will ensure it executes after the dataSource array is populated.

Also, the two observe events in the code are Single events - meaning they don't stay attached to the node so the removeAllObservers is extraneous.

    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)
                }  
            })
        }
    })

The above code is tested and works so if the tableview is still not displaying the data, there may be an issue in the tableView delegate functions.

这篇关于表格视图不显示来自Firebase的评论或用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆