Why does my code do the OnComplete first and the OnSucces later even when OnComplete is above in the code?



Using OnComplete I want to retrieve my documentid. I need this docid inside the OnSucces. So it has to go in that order. Why hadn’t it?

LesAdapter.java, with the documentID hardcoded to get good LOGS

 fStore.collection("Lessen").whereEqualTo("Wanneer", model.getWanneer())
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                           @Override
                                           public void onComplete(@NonNull @NotNull Task<QuerySnapshot> task) {
                                               if (task.isSuccessful()) {
                                                   for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                                                       docid = document.getId();
                                                       Log.d(TAG, "onComplete: docid =  " + docid);
                                                   }
                                               }
                                           }
                                       });
    fStore
            .collection("Lessen").document("IYedtghMuNI9fmrlJjX2").
            collection("Deelnemers").document(userID)
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                      @Override
                                      public void onSuccess(DocumentSnapshot documentSnapshot) {
                                          Log.d(TAG, "onSuccess: lid ingeschreven?");
                                      }
                                  })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull @NotNull Exception e) {
                    Log.d(TAG, "onFailure: lid niet ingeschreven?");
                }
            });

LOGS: enter image description here

Answer

I’m assuming, and I could be wrong, but onComplete triggers as soon as the operation has completed, regardless of whether or not it was successful.

That is why I’d imagine that onComplete would trigger before onSuccess.

the order of your code here won’t make a difference, because these are callbacks.



Source: stackoverflow