I have a Firebase database that looks something like this. It should be noted that I create the artCreated
value by getting epoch time and subtracting it from zero:
I am trying to get the most recent submission, but I can never get the order to reverse. Here is my code:
public class RecentArtRecyclerViewAdapter extends RecyclerView.Adapter<RecentArtRecyclerViewAdapter.ArtworkViewHolder> {
//List<ArtworkModel> artwork;
private Context context;
private DatabaseReference dbReference;
private ChildEventListener mChildEventListener;
ArrayList<String> artworkKeys = new ArrayList<>();
ArrayList<ArtworkModel> artwork = new ArrayList<>();
public RecentArtRecyclerViewAdapter(Context context, DatabaseReference dbReference){
this.context = context;
this.dbReference = dbReference;
dbReference.limitToFirst(100);
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "Artwork added...");
ArtworkModel artworkModel = dataSnapshot.getValue(ArtworkModel.class);
artwork.add(artworkModel);
artworkKeys.add(dataSnapshot.getKey());
notifyItemInserted(artwork.size()-1);
}
I pass in the DB reference, which looks like:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
final DatabaseReference artRef = databaseReference.child("artwork");
I have tried to get the reverse order (most recent) by doing things like:
dbReference.limitToFirst(100);
dbReference.limitToLast(100);
dbReference.orderByChild("artCreated");
dbReference.orderByChild("artCreated").limitToLast(10);
dbReference.orderByChild("artCreated").limitToFirst(10);
I have also tried changing the value of the artCreated
which started with everything being a negative number (smallest to largest) and also doing one negative number, and the rest positive (largest to smallest) and got the same result. The artCreated
key seems to have no impact on the order.
And nothing is working. It is always in the same order that it is stored in the database. What am I doing wrong. I have referenced a number of answers on this, but nothing is working for me.