I would like to retrieve documents from FireCloud. I would like to perform sorting after retrieving all of the documents. I found that sometimes it was unable to sort correctly due to the android did not wait until the Firebase sorted all of the documents. Is there any way to ensure that the Firestore retreived all of the document?
If did not have anyways to ensure that the Firestore retreived all of the documents. Can I able to combine two queries into one using or method and perform searching? Hi, I would like to retrieve documents from FireCloud. I would like to perform sorting after retrieving all of the documents. I found that sometimes it was unable to sort correctly due to the android did not wait until the firebase sort all of the document. Is there any ways to ensure that the firecloud retreived all of the document.
If did not have anyways to ensure that the firecloud retreived all of documents. Can I able to combine two query into one using or method and perform searching?
CollectionReference doc = firebaseFirestore.collection("TransferRecord");
Query query = doc.whereEqualTo("fromStudentID", StudentID)
.whereGreaterThanOrEqualTo("timeStamp", previousDate)
.whereLessThan("timeStamp", currentDate)
.orderBy("timeStamp", Query.Direction.DESCENDING);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot documentSnapshotA : task.getResult()) {
list = documentSnapshotA.toObject(TransferRecordClass.class);
transferrecordlist.add(list);
}
} else {
}
}
});
CollectionReference doc2 = firebaseFirestore.collection("TransferRecord");
Query query2 = doc2.whereEqualTo("toStudentID", StudentID)
.whereGreaterThanOrEqualTo("timeStamp", previousDate)
.whereLessThan("timeStamp", currentDate)
.orderBy("timeStamp", Query.Direction.DESCENDING);
query2.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot documentSnapshot : task.getResult()) {
list = documentSnapshot.toObject(TransferRecordClass.class);
transferrecordlist.add(list);
}
if (transferrecordlist.isEmpty()) {
Toast.makeText(TransferRecordActivity.this, "Empty", Toast.LENGTH_SHORT).show();
ImageNoFound.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
} else {
ImageNoFound.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
Collections.sort(transferrecordlist, new Comparator<TransferRecordClass>() {
@Override
public int compare(TransferRecordClass transferRecordClass1, TransferRecordClass transferRecordClass2) {
return transferRecordClass2.getTimeStamp().compareTo(transferRecordClass1.getTimeStamp());
}
});
mytransferrecordadapter = new TransferRecordAdapter(transferrecordlist, TransferRecordActivity.this, StudentID);
recyclerView.setAdapter(mytransferrecordadapter);
}
} else {
}
}
});
CodePudding user response:
If you want to perform two separate queries and later use the combined result, then I recommend you use Tasks#whenAllSuccess(Task...<?> tasks) method. So in code, it should look like this:
CollectionReference doc = firebaseFirestore.collection("TransferRecord");
Query query = doc.whereEqualTo("fromStudentID", StudentID)
.whereGreaterThanOrEqualTo("timeStamp", previousDate)
.whereLessThan("timeStamp", currentDate)
.orderBy("timeStamp", Query.Direction.DESCENDING);
CollectionReference doc2 = firebaseFirestore.collection("TransferRecord");
Query query2 = doc2.whereEqualTo("toStudentID", StudentID)
.whereGreaterThanOrEqualTo("timeStamp", previousDate)
.whereLessThan("timeStamp", currentDate)
.orderBy("timeStamp", Query.Direction.DESCENDING);
Task firstTask = query.get();
Task secondTask = doc2.get();
Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your list of objects.
}
});