I’m trying to load data from my firebase Firestore database. This code uses an OnSuccessListener which runs async so I’m trying to wait till it loads data to continue. I have done this by creating a while loop that runs until the OnSuccess function sets the variable to true.
Here is the code
public FragmentData getFragData(String fragType)
{
cont = false;
Log.d("useremail", currentUser.getEmail());
DocumentReference docRef = db.collection("users").document(currentUser.getEmail()).collection("FragmentData").document(fragType);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
returnData = documentSnapshot.toObject(FragmentData.class);
cont = true;
Log.d("testhi", "" + returnData.getId().get(0));
}
});
while(!cont);
//find a way to fix the fact that onsuccess doesnt process in time so return data is always null
Log.d("testhi", "" + returnData.getId().get(0));
return returnData;
}
When I do this, the while loop stops the OnSuccess Function from happening but I am wondering why since the OnSuccess function should be working async from the normal function. If there’s no way to salvage this I am wondering how I should go about this since if I don’t do this, returnData is null since it doesn’t load in time.