Java Android Firestore Make an onComplete and FirestoreRecyclerAdapter synchronous

Viewed 52

I am developing my social network home screen. In this screen will appear the posts of the users who follow the user who is authenticated. So I took the users following the user from the FireStore and put it in an arraylist:

FirebaseFirestore.getInstance().collection("following").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("userFollowing").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        document.getData();
                        Uidrecord.add(cont,String.valueOf(document.getData()));
                        cont++;
                    }
                    int conta = 0;
                    for (String item: Uidrecord){
                        if(item == null) {
                            Toast.makeText(MainActivity.this, "null item", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        uidFollowing.add(conta, item);
                        //Toast.makeText(MainActivity.this, uidFollowing.get(conta), Toast.LENGTH_SHORT).show();
                        conta++;

                    }
                    Toast.makeText(MainActivity.this, uidFollowing.size() + "", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        Toast.makeText(MainActivity.this, uidFollowing.isEmpty() + "", Toast.LENGTH_SHORT).show();

At this point, I thought of building a for that would repeat the clicks as many times as the length of the array list and then to make the query dynamic, because at each loop the query had a user where to get the posts:

 for(int i = 0; i<uidFollowing.size(); i++) {

            Query query = FirebaseFirestore.getInstance()
                    .collection("post/" + uidFollowing.get(i) + "/userPosts")
                    .limit(1000);

        FirestoreRecyclerOptions<GetInfoPost> option[.. etc]

The problem is that uidFollowing has not yet been assigned when the query takes the values of it (even if the OnComplete code block is first of all the code to extract the posts from the Firestore), so it appears null, this is because onComplete is asynchronous .

I also tried putting all the for code inside OnComplete like this https://codeshare.io/Lwno6e but running from this error:

2021-06-14 11: 25: 11.666 11745-11745 / com.conta.pophome E / AndroidRuntime: FATAL EXCEPTION: main Process: com.conta.pophome, PID: 11745 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.firebase.ui.firestore.FirestoreRecyclerAdapter.startListening ()' on a null object reference at com.conta.pophome.MainActivity.onStart (MainActivity.java:467)

So, is there a way to Make onComplete and FirestoreRecyclerAdapter synchronous?

0 Answers
Related