I'm trying to sort out trending posts by sorting them by time and karma using the following code:
FirebaseFirestore.getInstance().collection("topics")
.orderBy("time", com.google.firebase.firestore.Query.Direction.DESCENDING)
.whereGreaterThanOrEqualTo("time",(System.currentTimeMillis() % 1000)-1000*60*60*24*7)
.orderBy("karma", com.google.firebase.firestore.Query.Direction.DESCENDING).startAfter(currentDoc).limit(10).get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
for (DocumentSnapshot d : documentSnapshots.getDocuments()){
Topic topic = new Topic();
topic.setId(d.getId());
topic.setUsername(d.getString("username"));
topic.setCaption(d.getString("caption"));
topic.setPic(d.getString("pic"));
topic.setTime(d.getLong("time"));
topic.setType(d.getString("type"));
topic.setImage(d.getString("image"));
topics.add(topic);
if (topics.size()==documentSnapshots.size()){
currentDoc = d;
adapter.updateList(topics);
loading.setVisibility(View.GONE);
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
I added the Index in Firestore. But it's not working.
I mean it's showing 7 days old posts. But it's not sorted by karma. It's showing 7 days old posts sorted by time. And if i remove the orderBy("time") from the query. It's crashing.
Need Help :(