Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to traverse the children of a Firebase node in reverse order?

Tags:

firebase

I'm looking into implementing an in-firebase index as described here: https://stackoverflow.com/a/10559689/534086, and trying to figure out how to deal with sorting data in descending order. Is it possible if you have a node with children 'a', 'b', 'c' to retrieve them 'c', 'b', 'a'? (while still being able to retrieve them in the original order as well?)

If it's not possible to traverse the node both forwards and backwards and I need to create two indexes, is their an easy algorithm for generating keys that sort in reverse lexographic order? For numbers, I think you can multiply by -1, but not sure how to handle strings...

like image 503
josh Avatar asked Feb 28 '13 02:02

josh


People also ask

What method is used to populate a Firebase reference with data?

Use the push() method to append data to a list in multiuser applications. The push() method generates a unique key every time a new child is added to the specified Firebase reference.

Can you store arrays in Firebase?

We could create this data by sending the following JSON tree to the player's collection. This is because Firebase does not support Arrays directly, but it creates a list of objects with integers as key names.

How do I access data from Firebase?

Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes.


1 Answers

It sounds like what you want is a "reverse()" query similar to how "limit()" works now. Firebase can't do this yet, but we're planning to add it.

Depending on your use case, it might be pretty simple for you to get around this though. If, for instance, you're using Firebase's sorting to keep a list in order that is being displayed to the user, you could simply modify the display logic to render things backward (prepend items instead of append for instance). If you're querying to select a window of data out of a large set of children (say, to get the first 10 items out of a long list), the sort order shouldn't be important -- your start and end points for the query will remain the same except they'll be reversed.

like image 63
Andrew Lee Avatar answered Sep 28 '22 09:09

Andrew Lee