How to retrieve data from Firebase database in descending order?
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.
Sahaj Rana
College student, Android, Web full-stack and a Machine Learning Beginner! "Make dreams, the reality!"
Updated on July 15, 2022Comments
-
Sahaj Rana almost 2 years
I was wondering If new firebase database has added this capability because I need it in my project..
The structure looks like this..
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);
-
Sahaj Rana almost 8 yearsthat sucks.. now I have to save data first
-
TomerBu almost 8 yearsYap. you should filter most on the server, do the rest on the client.
-
Sahaj Rana almost 8 yearsthat's what i wanted to do, thanks by the way