I am running code similar to the problem in this question except, I am fetching arrays from each document and adding their elements to a global ArrayList. After that, outside of the collection.get()
, I perform an operation on that array and return. However, If I Log.d the global ArrayList size from within the onSuccess listener, it gives the correct answer but if I do so outside of the collection.get() it always gives 0. My problem is that I cannot return from within the onSuccess because this will not return for my whole function and so I need to do it after the collection.get() where the ArrayList has not been populate, presumably since the get() operation returns a Task<>
which is an asynchronous operation and so it doesn't wait for it to complete. Here is my code:
public static boolean Validator(final String sometext) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
final ArrayList<String> mArrayList = new ArrayList<>();
db.collection("my_collection").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (queryDocumentSnapshots.isEmpty()) {
Log.d(TAG, "Validator onSuccess: LIST EMPTY");
return;
} else {
// Convert the Query Snapshot to a list of Document Snapshots, in this
// case, one document for each my_collection
List<DocumentSnapshot> my_collections = queryDocumentSnapshots.getDocuments();
for (int i = 0; i < my_collections.size(); i++) {
ArrayList<String> mArrayitems = (ArrayList<String>) my_collections.get(i).get("array");
for (int j = 0; j < mArrayitems.size(); j++) {
Log.d(TAG, "Adding array: " + mArrayitems.get(j).toString());
mArrayList.add(mArrayitems.get(j).toString());
}
}
Log.d(TAG, "Validator onSuccess: array list built");
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Validator onFailure: " + e);
}
});
// check if the given sometext matches a array in the list
Log.d(TAG, "array list size: " + mArrayList.size());
if (mArrayList.size() > 0) {
for (int i = 0; i < mArrayList.size(); i++) {
if (sometext.endsWith(mArrayList.get(i))) {
return true
}
}
}
return false;
}
EDIT:
I'm trying to do this with a callback which looks like:
// constructor
public Validator(String sometext) {
this.valid = false;
this.sometext = sometext
}
public interface myCallback{
void onCallback(boolean valid);
}
public boolean validate() {
readData(new MyCallback() {
@Override
public void onCallback(boolean valid) {
setValid(valid);
}
});
return this.valid;
}
private void readData(final MyCallback myCallback) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("my_collection").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (queryDocumentSnapshots.isEmpty()) {
Log.d(TAG, "Validator onSuccess: LIST EMPTY");
} else {
List<DocumentSnapshot> my_collections = queryDocumentSnapshots.getDocuments();
ArrayList<String> mArrayList = new ArrayList<>();
for (int i = 0; i < my_collections.size(); i++) {
ArrayList<String> mArrayItems = (ArrayList<String>) my_collections.get(i).get("array");
for (int j = 0; j < mArrayItems.size(); j++) {
Log.d(TAG, "Adding array: " + mArrayItems.get(j).toString());
mArrayList.add(mArrayItems.get(j).toString());
}
}
boolean result = false;
Log.d(TAG, "array list size: " + mArrayList.size());
if (mArrayList.size() > 0) {
for (int i = 0; i < mArrayList.size(); i++) {
if (sometext.endsWith(mArrayList.get(i))) {
result = true;
Log.d(TAG, "MATCH FOUND");
break;
}
}
}
myCallback.onCallback(result);
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Validator onFailure: " + e);
}
});
}
The problem I'm having is that in validate() I want to return the correct valid value after the callback is finished because it currently returns the incorrect value before the callback has finished.