1

I am developing the home of my Social Network; so, to view the posts of the following users I have to insert all the following users on an array list. The following variables are declared globally:

private ArrayList<String> Uidrecord = new ArrayList<>();
private ArrayList<String> uidFollowing = new ArrayList<>(); 

I'm doing it like this: (This is on the oncreate)

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();
                }
            }
        });

the problem is that when I go to do (outside the addOnCompleteListener method but always in the oncreate):

uidFollowing.get(0) 

the returned value is null.

Do you know how I could solve this problem?

PopHome2
  • 35
  • 6
  • Data is loaded from Firestore asynchronously and by the time your `uidFollowing.get(0)` executes, the `uidFollowing.add(conta, item)` hasn't been called yet. I recommend verifying this by running the code in a debugger, or by checking the order of some log messages on the lines before/after those statements. For an explanation and how to deal with this asynchronous behavior, see https://stackoverflow.com/a/51002413/209103 – Frank van Puffelen Jun 13 '21 at 21:51

1 Answers1

1

Yes you will get null because addOnCompleteListener is async method,if you call get() method outside addOnCompleteListener, get() method will executed before onComplete() .if you want item at 0 call right after add().

  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++;

                    }

log.i("following",String.valueOf(uidFollowing.get(0)))
androidLearner
  • 1,654
  • 1
  • 7
  • 13
  • So there is no way to call the get out of the method? because I need the get for a firestore query and I have to use recyclerview. This is my complete code https://codeshare.io/WdN6pM – PopHome2 Jun 14 '21 at 09:13
  • I tried to put the recyclerview inside the oncomplete like this https://codeshare.io/Lwno6e but then it gives me the following 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)` – PopHome2 Jun 14 '21 at 09:26
  • All the work that rely on "uidFollowing" list should be inside onComplete.@PopHome2 – androidLearner Jun 14 '21 at 13:35
  • Unfortunately I can't just put the query on onComplete because I need a for that also includes the Recyclerview code. I have moved on to this question for this problem->https://stackoverflow.com/questions/67971227/java-android-firestore-make-an-oncomplete-and-firestorerecycleradapter-synchrono – PopHome2 Jun 14 '21 at 13:45