So the issue I am having is that the loadUserNameFromFirestore() method executes the return statement before the firestore db.collection.get() query finishes executing. I have put logs after the firestore db.collection.get() query, and before the method return statement.
private String loadUserNameFromFirestore(Appointment appointment) {
final StringBuilder userFullName = new StringBuilder();
String userUid = appointment.getAppointment_doctor_uid();
db.collection(Constants.FIRESTORE_DOCTOR).document(userUid)
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot documentSnapshot = task.getResult();
userFullName.append(documentSnapshot.getString(Constants.FIRESTORE_STAFF_TITLE)).append(" ");
userFullName.append(documentSnapshot.getString(Constants.FIRESTORE_STAFF_FNAME)).append(" ");
userFullName.append(documentSnapshot.getString(Constants.FIRESTORE_STAFF_LNAME));
Log.d("DEBUG_APP", userFullName.toString());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
Log.d("DEBUG_APP", "Final: " + userFullName.toString());
return userFullName.toString();
}
Below are the logs created from the above code.
10-03 02:43:30.561....: Final:
10-03 02:43:31.044....: Dr Naila Alam
As you can see above, the log before the return statement, "First: ", was executed first, then the log after the firestore query, "Dr Naila Alam".
Is there a way to make sure the return statement doesn't get called until after the firestore finishes executing. I would really appreciate any help.