0

This is my UserMaganamentFragment:

private static final String TAG = "UserManagamentFragment";
private List<String> mEmails  = new ArrayList<>();
private FirebaseFirestore db;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_user_managament, container, false);

    getEmails();

    RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter( mEmails, getContext());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    return view;
}

private void getEmails(){

    db = FirebaseFirestore.getInstance();

    db.collection("users")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            mEmails.add(document.getData().get("email").toString());
                        }
                    }
                }
            });

` I even tried adding everything in the OnCreateView function but it still didn't affect the mEmails arrayList.

mEmail is full with data at the end of the getEmails function.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
breezy11
  • 1
  • 4
  • Looks like getEmails executed asynchornously. basically the rest of code is executed before onComplete function is called. In documentation you can find code for blocking operation: https://firebase.google.com/docs/firestore/query-data/get-data – Vlad Bochenin Mar 18 '20 at 23:04
  • All read from/write to Firestore is asynchronous. There is no blocking operation. – Frank van Puffelen Mar 18 '20 at 23:22
  • please post your adapter class – Edgar Mar 18 '20 at 23:59
  • As FrankvanPuffelen mentioned in his comment, all Firestore operations are asynchronous. Please check the duplicate to see why do you have this behavior and how can you solve this using a custom callback. – Alex Mamo Mar 19 '20 at 10:03

0 Answers0