I need a function to get all data/notes from FirebaseFirestore. How do I make this function wait for all data before return?
I think this function I created is not working in the main thread and return before getting data from firebase
public static ArrayList<NoteFB> getNotes() {
FirebaseFirestore db = FirebaseFirestore.getInstance();
final String TAG = "FB Adapter";
final ArrayList<NoteFB> doFBs = new ArrayList<>();
db.collection("notesItem")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
doFBs.add(document.toObject(NoteFB.class));
}
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
return doFBs;
}
MyFBAdapter myFBAdapter = new MyFBAdapter(ShowActivity.this, FBAdapter.getNotes());
rvContacts.setAdapter(myFBAdapter);
This code returns an empty ArrayList that creates an empty recyclerview.