1

I have a list of numbers inside that have certain data, in which I am adding a value "Sí" or "No", as in this image:

enter image description here

The last "Sí" that I added was the one that is in number 4, but if I do a filter in Firebase with

.equalTo("Sí").limitToLast(1)

I return the value of "Sí" positioned in the number 5 and not in the 4 that was the last "Sí" that I added to the database. There is some way to recognize the last "Sí", without the need of that is in the last position of the list?

I still can not find the solution to this, I hope you can help me.

Thank you.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

1

There is some way to recognize the last "Sí", without the need of that is in the last position of the list?

Yes there is. The most common approach would be to add under each object a new property of type Timestamp named lastUpdate and then query descending according to it. Everytime you update a value, change the value also to lastUpdate with the current timestamp. This is how your schema might look like:

Firebase-root
  |
  --- Users
        |
        --- 1
        |   | 
        |   --- Activo: "Si"
        |   |
        |   --- lastUpdate: 1561202277
        |
        --- 2
            | 
            --- Activo: "No"
            |
            --- lastUpdate: 1561202299

This is how to save the timestamp:

And this is how to order descending:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193