我正在尝试从我的Firebase数据库中检索帖子列表,但是它不起作用。我遇到麻烦的特定代码段是line ref.child("users").queryOrderedByKey().observeSingleEvent
。当我调试应用程序时,它只是跳过了这一行,应用程序跳到了ref.removeAllObservers()
。我在调用正确的函数以从数据库中检索吗?我正在使用的教程来自Swift3,因此它可能已过时。
我尝试调用observe而不是observeSingleEvent,但结果相同。
这是我的fetchPosts函数:
func fetchPosts(){
let ref = Database.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: {snapshot in
let users = snapshot.value as! [String : AnyObject]
for (_,value) in users {
if let uid = value["uid"] as? String{
if uid == Auth.auth().currentUser?.uid {
if let followingUsers = value["following"] as? [String: String]{
for(_, user) in followingUsers {
self.following.append(user)
}
}
self.following.append(Auth.auth().currentUser!.uid)
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: {(snap) in
let postsSnap = snap.value as! [String : AnyObject]
for(_, post) in postsSnap {
if let userID = post["userID"] as? String{
for each in self.following{
if each == userID{
let posst = Post()
if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String{
posst.author = author
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
self.posts.append(posst)
}
}
}
self.collectionview.reloadData()
}
}
})
}
}
}
})
ref.removeAllObservers()
}
我希望用Firebase数据库中的帖子信息填充收藏夹视图。没有错误消息。