0

I tried with this code to sort my posts by timestamp it doesn't work, each time I launch the simulator the order of the cells is different, I suppose this isn't the way to do it, could somebody explain me where I am wrong... I edited the code, now my problem is that the most recent posts are displayed at the bottom and I would like them to to be displayed at the top

        self.user.removeAll()
        for child in DataSnapshot.children.allObjects as! [DataSnapshot] {
            print("Processing user \(child.key)")

            let value = child.value as? NSDictionary
            //if country == "UNITED STATES"{
            if let uid = value?["userID"] as? String{
                if uid != Auth.auth().currentUser!.uid {
                    //
                    let userToShow = User()
                    if let fullName = value?["username"] as? String , let imagePath = value?["photoURL"] as? String{
                        userToShow.username = fullName
                        userToShow.imagePath = imagePath
                        userToShow.userID = uid
                        self.user.append(userToShow)
                    }

                }
            }
        }
  • "doesn't work" is not a sufficient description of your issue. Please [edit] your question (don't post a comment) to clearly explain in what way exactly that the code does not work as expected. – rmaddy Feb 20 '19 at 19:52

1 Answers1

0

As soon as you call DataSnapshot.value, you're converting the data in the snapshot into a dictionary. And the order if keys in that dictionary is not guaranteed.

To maintain the order of the elements as they come back from the database, you need to loop over DataSnapshot.children. See these questions for examples of how to do that:


For your code this would look something like:

ref.child("users").queryOrdered(byChild: "timestamp").observeSingleEvent(of: .value, with: { snapshot in

    self.user.removeAll()

    for child in snapshot.children.allObjects as [DataSnapshot] {
        print("Processing user \(child.key)")

        let value = child.value as? NSDictionary

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


    self.tableview.reloadData()
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for your answer but I really don't understand how to put this inside my code, could you please tell me more about how to do this ? –  Feb 20 '19 at 22:19
  • I added an example. If you can't get that to work, edit your question to show what you've tried. Note that we only care about the [minimum code that reproduces the problem you'r asking about](http://stackoverflow.com/help/mcve). Since your question is about ordering, a lot of the contents inside the loop are going to be irrelevant to that (hence my only printing the key above). – Frank van Puffelen Feb 21 '19 at 14:10
  • Thanks for your answer, _I edited the code, now my problem is that the most recent posts are displayed at the bottom and I would like them to to be displayed at the top, it is probably really simple but I can't figure out out to do it_ –  Feb 21 '19 at 18:49
  • You'll need to reverse the children client-side. See https://stackoverflow.com/q/36314374, https://stackoverflow.com/q/43336976, https://stackoverflow.com/q/43252794 and probable some more from this list: https://www.google.com/search?q=site:stackoverflow.com+swift+firebase+reverse+order – Frank van Puffelen Feb 21 '19 at 18:58
  • Thanks a lot, I fixed my problem –  Feb 21 '19 at 19:05