0

I'm new using Firebase and I can't find how to do what sounds really simple to do : list all the latest entries of my database.

Here is a screenshot of what my database looks like :

database

So, I'm trying to list the latest entries like that :

    // picturesRef = FIRDatabase.database().reference().child("pictures")
    let _ = self.picturesRef.queryOrdered(byChild: "createdTime").queryLimited(toLast: 50).observe(.value, with: { snapshot in
        // Stocking the result into picturesArr array
        for elem in picturesArr {
            print(elem.createdTime)
        }
    })

And right now, when I'm displaying the createdTime value of each item, I have something like :

  • 1484738582.0
  • 1484000086.0
  • 1484738279.0
  • 1484734358.0
  • 1484625525.0
  • 1484728677.0

Which doesn't seem to be ordered from the oldest entry to the newest one...

Also, when I replace "createdTime" in the query by "fieldThatDoesntExist", I have the exact same result.

Anyone would know where did I do something wrong in the code ? Thanks in advance !

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mika
  • 35
  • 8
  • I solve it changing my date format, I use it like 2016/12/23, 2016/12/13, 2017,01/16. Im seeing your code correct. But I don't know the time format you are using and if that works for sorting it in order. – Mago Nicolas Palacios Jan 20 '17 at 14:06
  • Thanks @MagoNicolasPalacios for your answer. I suppose that the date format shouldn't be a problem because it stays a Double variable, the query should order the results from the biggest Double to the smallest one. i still think that something is not correct in my query :-/ – Mika Jan 20 '17 at 14:15
  • Maybe you are right, And you need to First to enter de to the ID child you have, and then make the query ordered by child. – Mago Nicolas Palacios Jan 20 '17 at 14:18
  • Yeah I was thinking about that too, but I couldn't find out how to dynamically get the keys of the entries that I'm trying to list... – Mika Jan 20 '17 at 14:22
  • You should first enter a for each loop of the childs of pictures to have all their ID's, and then order them by date. – Mago Nicolas Palacios Jan 20 '17 at 14:25

2 Answers2

1

The query returns the items in the correct order. But most likely (the relevant code seems to be missing from your question) you're losing that order when you convert the snapshot to a dictionary (which is unordered by definition).

To keep the items in the correct order, iterate over snapshot.children:

let picturesRef = FIRDatabase.database().reference().child("pictures")
let _ = picturesRef
  .queryOrdered(byChild: "createdTime")
  .queryLimited(toLast: 50)
  .observe(.value, with: { snapshot in
      for child in snapshot.children {
          print(child.key)
          print(child.child("createdTime").value
      }        
})

Also see:

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

Actualy, after sorting the array returned by the query with this :

picturesArr.sort(by: {$0.createdTime > $1.createdTime})

I've figured out that the query returns the entries that I'm looking for but not sorted.

It looks a bit wierd to me, maybe someone knows why or even better, how to get the result already sorted ?

Mika
  • 35
  • 8