简体   繁体   English

Firebase queryOrderedByKey无法按时间排序帖子

[英]Firebase queryOrderedByKey not sorting posts chronologically

My app has a collection view which displays rows of photos/images from Firebase, and I'd like to have them load in the order they were added, with the newest posts at the top. 我的应用程序有一个收藏夹视图,其中显示了Firebase中的照片/图像行,我希望按照添加顺序来加载它们,最新的帖子放在顶部。 I thought that using queryOrderedByKey did that by default, and that's what I used in my fetch function, but the posts are out of order. 我以为默认情况下使用queryOrderedByKey做到这一点,这就是我在提取函数中使用的功能,但是帖子queryOrderedByKey

This is how I'm fetching the posts currently: 这是我目前提取帖子的方式:

func fetchPosts() {

    let ref = FIRDatabase.database().reference()
    ref.child("users").queryOrderedByKey().observe(.value, with: { snapshot in

        let users = snapshot.value as! [String : AnyObject]

        for (_, value) in users {

            if let uid = value["uid"] as? String {

                if uid == FIRAuth.auth()?.currentUser?.uid {

                    if let followingUsers = value["following"] as? [String : String] {

                        for (_, user) in followingUsers {
                            self.following.append(user)
                        }
                    }
                    self.following.append(FIRAuth.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 poster = post["poster"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {

                                            posst.poster = poster
                                            posst.likes = likes
                                            posst.pathToImage = pathToImage
                                            posst.postID = postID
                                            posst.userID = userID
                                            if let people = post["peopleWhoLike"] as? [String : AnyObject] {
                                                for (_, person) in people {
                                                    posst.peopleWhoLike.append(person as! String)
                                                }
                                            }
                                            posts.append(posst)
                                        }
                                    }
                                }
                                self.collectionView.reloadData()
                            }
                        }
                    })
                    ref.removeAllObservers()
                }
            }
        }
    })
}

How can I get the posts to sort by newest first? 我怎样才能使帖子按最新的顺序排序?

EDIT 2: updated - now sorting from oldest -> newest 编辑2:更新-现在从最旧的->最新的排序

func fetchPosts() {

    let ref = FIRDatabase.database().reference()
    ref.child("users").queryOrderedByKey().observe(.value, with: { snapshot in

        let users = snapshot.value as! [String : AnyObject]

        for (_, value) in users {

            if let uid = value["uid"] as? String {

                if uid == FIRAuth.auth()?.currentUser?.uid {

                    if let followingUsers = value["following"] as? [String : String] {

                        for (_, user) in followingUsers {
                            self.following.append(user)
                        }
                    }
                    self.following.append(FIRAuth.auth()!.currentUser!.uid)

                    ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in

                        for postSnapshot in snap.children.allObjects as! [FIRDataSnapshot] {
                            let value = postSnapshot.value as! [String : AnyObject]

                            if let userID = value["userID"] as? String {
                                for each in self.following {
                                    if each == userID {

                                        let posst = Post()
                                        if let poster = value["poster"] as? String, let likes = value["likes"] as? Int, let pathToImage = value["pathToImage"] as? String, let postID = value["postID"] as? String {

                                            posst.poster = poster
                                            posst.likes = likes
                                            posst.pathToImage = pathToImage
                                            posst.postID = postID
                                            posst.userID = userID
                                            if let people = value["peopleWhoLike"] as? [String : AnyObject] {
                                                for (_, person) in people {
                                                    posst.peopleWhoLike.append(person as! String)
                                                }
                                            }
                                            posts.append(posst)
                                        }
                                    }
                                }
                                self.collectionView.reloadData()
                            }
                        }
                    })
                    ref.removeAllObservers()
                }
            }
        }
    })
}

Queries return a snapshot the matching child nodes in the order requested. 查询以请求的顺序返回匹配子节点的快照。 That means that the results consists of three things: 这意味着结果包括三件事:

  1. the item keys 项目键
  2. the item values 项目值
  3. the order of the items relative to each other 项目相对于彼此的顺序

But then the first thing you do with the snapshot is: 但是,您对快照所做的第一件事是:

ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
  let postsSnap = snap.value as! [String : AnyObject]

You convert the snapshot into a dictionary. 您将快照转换为字典。 And a dictionary only has space for two things: the keys and the values. 字典只有两件事的空间:键和值。 So the order is lost at this point, since dictionaries have an undefined order. 由于字典具有未定义的顺序,因此此时顺序丢失了。

The proper way to access the result in the order you requested is to use the snapshot's built-in collection of children to iterate over them: 按您请求的顺序访问结果的正确方法是使用快照的内置子项集合对其进行迭代:

ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

  for postSnapshot in snapshot.children {
    let value = postSnapshot.value as! [String : AnyObject]

This will loop over the matching children in the order you queries them. 这将按照查询子项的顺序遍历匹配的子项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM