I have data that should be visible in code that I get from FireStore collection and add it to ArrayList, I know that it happens because get() method returns immediately so the query isn't done yet, and hence the onCreate() method won't see any data, but is there a way to save that data in ArrayList and use it somewhere else?
For example here is two versions of code: 1st one make Toast with whole list like it should:
db.collection("Users Data")
.document(currentUserID)
.collection("Follows")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
following.add(document.getId());
Toast.makeText(SearchedProfile.this, following.toString(), Toast.LENGTH_SHORT).show();
}
}
}
});
and 2nd make Toast with empty ArrayList (Toast says '[]')
db.collection("Users Data")
.document(currentUserID)
.collection("Follows")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
following.add(document.getId());
}
}
}
});
Toast.makeText(SearchedProfile.this, following.toString(), Toast.LENGTH_SHORT).show();
Only difference is a position of Toast method.