I am trying to order and filter the data using firebase realtime database. In one case I want to use OrderByKey (because I want new questions first and keys are generated using push feature) to sort the data, in another case I am using OrderByChild (because I want to sort using timestamp and in this case I don't use push but I use uid of the user as key).
In both case I want the newest thing/person(question/user) first so I want to order in descending order(at client side I even reverse that). So this is how I query after first query. First query just doesn't has endAt(endAtKey)
part in below shown query.
For OrderByKey:
ref.child("questions").orderByKey().endAt(endAtKey).limitToLast(10);
//data at questions node looks like this
{
"-L1CbnCkXJiRT9MKpoP1" : {
"mCorrectAnswer" : 1,
"mOptionA" : "ueud",
"mOptionB" : "h",
"mOptionC" : "h",
"mOptionD" : "h",
"mProblem" : "4shsudududududududurududu"
},
"-L1Dd9x6ws5di2rf8yay" : {
"mCorrectAnswer" : 1,
"mOptionA" : "sh",
"mOptionB" : "shsh",
"mOptionC" : "hehs",
"mOptionD" : "hssh",
"mProblem" : "12ffsnsjsjdbxbddhdbdbddjebddb"
}
For OrderByChild:
mDatabaseReference.child("user_list").orderByChild("mTimeStamp")
.endAt(endAtKey).limitToLast(10);
//data at user_list reference looks like this
{
"VH78axip1bXN9dktsF4Az0oBche2":
"mTimeStamp": "1508782492973",
"mUserName": "Satyam Kumar"
},
"2MedW9KSYTcB8I1qp0xcoNuwPEf1":
"mTimeStamp": "1508823085614",
"mUserName": "Thirunagari Yeshwanth"
}
I want to populate the RecyclerView with the data in groups of 10 so in onDataChange
I save the key of first child received in onDataChange
method.
if (countCopy == 1) { // while iterating through children this variable tells
//the index of the loop.
endAtKey = currentKey;
Log.e("endKey", endAtKey);
}
Now the problem I am facing is in subsequent fetches of data using the query above I am receiving the endAtKey
fine(data received is also what is expected) for OrderByKey
query but endAtKey
is always the same for OrderByChild
query and the data being received is always the same.
How can I solve this problem?
The problem with your OrderByChild
query is that the endAtKey
that you're getting is a key, instead of a timestamp
. Remember that the OrderByChild is being used to query on the timestamp attribute, so your endAtKey
must be a timestamp and not a key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With