Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve data from Firebase database in descending order? [duplicate]

I was wondering If new firebase database has added this capability because I need it in my project..

The structure looks like this..

enter image description here

and ya setting RecyclerView to reverse wont work for me because that cause it cause it to start from bottom.

Solution

So, This is the best solution i can possibly think of..For now.

As @Frank said this could be the duplicate but I solved it initially with his help.

so What we can do is Make the timeStamp negative i.e -20164846476589

and then Subtracting it with Some higher value like 999999999999999L (which for now we are not going to exceed offcourse) and then what we get Would be the lowest descending value for new Timestamp

and the data would automagically will be descending in Firebase

ie

 String NewDescTimeStamp= String.valueOf((9999999999999L+(-1 * PreviousTimestamp)));

 mFirebaseRef.child(NewDescTimeStamp).setValue(true);
like image 645
Sahaj Rana Avatar asked Jul 11 '16 09:07

Sahaj Rana


1 Answers

You can't do that with the existing API.

You can use one or more of the following methods to retrieve a known number of children: limitToFirst(), limitToLast(), startAt(), endAt()

also you can use one of the ordering methods: orderByChild(), orderByValue() , orderByKey()

Than, Store them in a data structure (Such as ArrayList) and then Use java.util.Collections.reverse() to reverse the list.

With firebase you should do some of the query logic client side. Not all operations available in SQL are available on Firebase.

like image 76
TomerBu Avatar answered Sep 27 '22 18:09

TomerBu