1

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jeffrey
  • 37
  • 9

1 Answers1

4

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.

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • Thanks for the response. Oke assuming this is correct. Do you have any Idea's on how to work around that? – Jeffrey Jan 06 '21 at 08:36
  • i'd have to guess and say that you'd have to move your code into your onComplete listener, because you already have `if (task.isSuccessful()) {` in there, so you should be able to know whether or not it has succeeded and then get the relevant data from there – a_local_nobody Jan 06 '21 at 08:39
  • Thanks, thats works fine. But A little too good. .addOnSuccesListener always returns the LOG, even when I delete the document (userid) + whole collection (Deelnemers)... Do you know why the .OnFailureListener doesnt return then? Or should I start a new question? – Jeffrey Jan 06 '21 at 08:46
  • i'm not a firebase/firestore expert (there are quite a few of them on SO) so that's why i'm attempting to answer as safely as possible without giving you incorrect info :) but the success listener will always return as long as the _operation succeeded_ - it doesn't matter if it returns/finds results or not. the only time the failure listener will trigger is if the request entirely failed, for example if you had no internet connection or (i'd imagine) if you were perhaps trying to access something you don't have permission to. but you can ask a new question if you'd like – a_local_nobody Jan 06 '21 at 08:55
  • Many Thanks again! I try to play around for a little while and will ask another question if it doesn't workout. Have a nice day. – Jeffrey Jan 06 '21 at 08:59
  • no problem. numerous api's use this same format of using Complete/Success/Failure listeners (it's something you might implement yourself at some point) so it's worth learning and understanding, happy coding :) – a_local_nobody Jan 06 '21 at 09:02
  • @a_local_nobody Excellent answer, but there is something to notice, [Firestore SDK doesn't throw an error when there is no internet connection](https://stackoverflow.com/questions/61139056/what-happens-if-firestore-fails-to-save-data), and it makes sense since Firestore is designed to work offline. Behind the scenes, Firestore SDK tries to reconnect until the devices regain connectivity. However, it will indeed be triggered, when Firestore servers reject the request due to a security rule issue. – Alex Mamo Jan 07 '21 at 09:33
  • 1
    thank you for the info @AlexMamo :) i completely forgot about the offline functionality so what you said makes complete sense, i don't have too much experience with firebase so when i was thinking of a reason why an onFailure/onError would be triggered in general, the first obvious reason that came to mind was connectivity, i just forgot that it wouldn't be a problem with firebase :) – a_local_nobody Jan 07 '21 at 09:51