I am storing data in Firebase storage.
Object Comment
with attribute timestamp
. When I push data from device to Firebase I'm populating timestamp
with currentTime and store in long
data type.
When I do retrieving the data with firebaseRef.orderByChild("timestamp").limitToLast(15)
result is not sorting how I expected.
I even played around with rules and no result:
{
"rules": {
".read": true,
".write": true,
".indexOn": "streetrate",
"streetrate": {
".indexOn": ".value"
}
}
}
I tried store timestamp
in String
data type, same issue.
The @Monet_z_Polski approach, based on
does have a weird effect on not update FirebaseRecyclerView automatically (PopulateViewHolder is not triggered in realtime changes). So, the best option is use a negative key to index the data.
Swift 3:
Firebase can order the items in ascending order by a given property and then returns either the first N items (
limitToFirst()
) or the last N items (limitToLast()
). There is no way to indicate that you want the items in descending order.There are two options to get the behavior you want:
Use a Firebase query to get the correct data, then re-order it client-side
Add a field that has a descending value to the data
For the latter approach, it is common to have a inverted timestamp.
I don't see any option to reverse the data. But One Brute way is to get the data.
Sort at client side is simple and not require more system resources. Each data snapshot has previousChildkey field. If you want to desc sorting, imagine previousChildkey is nextChildKey. Here are my sample:
Sorting child items by TIMESTAMP can be done using
android.support.v7.util.SortedList
android.support.v7.util.SortedList
can also be used withRecyclerView