user9244682
user9244682

Reputation:

Get more recent data Firestore Android

I'm working on an App of the Social Network type on Android. My project is connected to Firebase. Everything works perfect. But I need that for example, the publications are returned from the most recent to the oldest. I'm using Cloud Firestore.

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference collectionReference = db.collection("eventos");

collectionReference.limit(2).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot documentSnapshots) {
        datos.removeAll(datos);

        for (DocumentSnapshot document : documentSnapshots.getDocuments()) {
            Map<String, Object> mapaEvento = document.getData();
            vento evento = new Evento((String) mapaEvento.get("usuario"), (String) mapaEvento.get("titulo"), (String) mapaEvento.get("categoria"));
            datos.add(evento);
         }
         adaptadorRecyclerHome.notifyDataSetChanged();
    }
});

Upvotes: 0

Views: 188

Answers (1)

user3773246
user3773246

Reputation:

How I would do it?

When you insert a JSON to firebase, add another field called timestamp that has the time of when it was inserted.

Then use Query to orderByChild('timestamp')

 Query query = db.child("eventos").orderByChild("timestamp");

Upvotes: 1

Related Questions