firestore onSuccess listener isn't working












0















I am trying to download some Quiz objects from my database.
The following function is called from onCreate of a certain activity.



 private void downloadQuizzesFromCloud(){

String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");
Query userQuizzes = db.collection(user_quizzes_path);

userQuizzes.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
quizzes.clear();
for (QueryDocumentSnapshot document : task.getResult()) {

Quiz quizDownloaded = getQuizFromCloud(document.getId());
quizzes.add(quizDownloaded);
}
Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
//TODO put in recycle adapter
} else { }
}
});

}


(user_quizzes_path contains the correct path to a collection of Quiz objects stored on the cloud)



I debugged this functions and found out that after the command:



 userQuizzes.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()


The function finishes execution, that is the onComplete cases aren't checked and executed and all this code is just skipped.



I tried to find this on the documentation of firebase but didn't find anything.
Why is this happening and how can I fix this?



Would appreciate some help here, thanks!










share|improve this question



























    0















    I am trying to download some Quiz objects from my database.
    The following function is called from onCreate of a certain activity.



     private void downloadQuizzesFromCloud(){

    String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
    final FirebaseFirestore db = FirebaseFirestore.getInstance();
    String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");
    Query userQuizzes = db.collection(user_quizzes_path);

    userQuizzes.get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
    if (task.isSuccessful()) {
    quizzes.clear();
    for (QueryDocumentSnapshot document : task.getResult()) {

    Quiz quizDownloaded = getQuizFromCloud(document.getId());
    quizzes.add(quizDownloaded);
    }
    Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
    //TODO put in recycle adapter
    } else { }
    }
    });

    }


    (user_quizzes_path contains the correct path to a collection of Quiz objects stored on the cloud)



    I debugged this functions and found out that after the command:



     userQuizzes.get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()


    The function finishes execution, that is the onComplete cases aren't checked and executed and all this code is just skipped.



    I tried to find this on the documentation of firebase but didn't find anything.
    Why is this happening and how can I fix this?



    Would appreciate some help here, thanks!










    share|improve this question

























      0












      0








      0








      I am trying to download some Quiz objects from my database.
      The following function is called from onCreate of a certain activity.



       private void downloadQuizzesFromCloud(){

      String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
      final FirebaseFirestore db = FirebaseFirestore.getInstance();
      String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");
      Query userQuizzes = db.collection(user_quizzes_path);

      userQuizzes.get()
      .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull Task<QuerySnapshot> task) {
      if (task.isSuccessful()) {
      quizzes.clear();
      for (QueryDocumentSnapshot document : task.getResult()) {

      Quiz quizDownloaded = getQuizFromCloud(document.getId());
      quizzes.add(quizDownloaded);
      }
      Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
      //TODO put in recycle adapter
      } else { }
      }
      });

      }


      (user_quizzes_path contains the correct path to a collection of Quiz objects stored on the cloud)



      I debugged this functions and found out that after the command:



       userQuizzes.get()
      .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()


      The function finishes execution, that is the onComplete cases aren't checked and executed and all this code is just skipped.



      I tried to find this on the documentation of firebase but didn't find anything.
      Why is this happening and how can I fix this?



      Would appreciate some help here, thanks!










      share|improve this question














      I am trying to download some Quiz objects from my database.
      The following function is called from onCreate of a certain activity.



       private void downloadQuizzesFromCloud(){

      String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
      final FirebaseFirestore db = FirebaseFirestore.getInstance();
      String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");
      Query userQuizzes = db.collection(user_quizzes_path);

      userQuizzes.get()
      .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull Task<QuerySnapshot> task) {
      if (task.isSuccessful()) {
      quizzes.clear();
      for (QueryDocumentSnapshot document : task.getResult()) {

      Quiz quizDownloaded = getQuizFromCloud(document.getId());
      quizzes.add(quizDownloaded);
      }
      Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
      //TODO put in recycle adapter
      } else { }
      }
      });

      }


      (user_quizzes_path contains the correct path to a collection of Quiz objects stored on the cloud)



      I debugged this functions and found out that after the command:



       userQuizzes.get()
      .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()


      The function finishes execution, that is the onComplete cases aren't checked and executed and all this code is just skipped.



      I tried to find this on the documentation of firebase but didn't find anything.
      Why is this happening and how can I fix this?



      Would appreciate some help here, thanks!







      android firebase google-cloud-firestore






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 18:10









      genericnamegenericname

      727




      727
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The onComplete is called when the read operation has completed from the Firestore servers. If it's not getting called, I can see two possible reasons:




          1. You're not connected to the server. Unless you've read the data before (and it's in the local database that the Firestore client maintains), this means the read never completes locally.

          2. You're not thinking asynchronously. Note that data is read from the server asynchronously, and there may be some time between when you call get() and when onComplete fires. To test if this is the case, put a breakpoint on if (task.isSuccessful()) { and run the app in the debugger. The breakpoint will hit when the data is read from the server.






          share|improve this answer
























          • Thank you, the problem is indeed because I am not thinking asynchronously. How can I make sure that objects I change in my code will change only after the server data read is complete?

            – genericname
            Dec 29 '18 at 8:35













          • You do that by calling the code from within onComplete, since that is the method that fires after the data has loaded.

            – Frank van Puffelen
            Dec 29 '18 at 15:11











          • this would work however if I were to use the variable "quizzes" somewhere else in my code, (for example just before the function "downloadQuizzesFromCloud" is finished) It is not promised that the task has finished and I could have incorrect data in "quizzes" variable. Is there a way to solve this?

            – genericname
            Dec 29 '18 at 15:16











          • Any code that depends on the quizes that are loaded asynchronously from the database should be either inside the onComplete (that is called when the data has loaded) or called from within that method. I commented the same on your previous question here: stackoverflow.com/questions/53970276/…

            – Frank van Puffelen
            Dec 29 '18 at 16:42





















          0














          Use a callback interface. Just like this below.



          private void downloadQuizzesFromCloud(Consumer listener) {

          String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();

          final FirebaseFirestore db = FirebaseFirestore.getInstance();

          String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");

          Query userQuizzes = db.collection(user_quizzes_path);

          userQuizzes.get()
          .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
          @Override
          public void onComplete(@NonNull Task<QuerySnapshot> task) {
          if (task.isSuccessful()) {
          List<Quiz> quizzes = new ArrayList<>();
          for (QueryDocumentSnapshot document : task.getResult()) {
          Quiz quizDownloaded = getQuizFromCloud(document.getId());
          quizzes.add(quizDownloaded);
          }
          listener.onGet(quizzes);
          Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
          //TODO put in recycle adapter
          } else { }
          }
          });

          }
          interface Consumer {
          void onGet(List<Quiz> quizzes);
          }





          share|improve this answer
























          • I want to acheive something like this: downloadQuizzesFromCloud(); print(quizzes); and I want that quizzes will be updated when I print them. How does can I do this with a callback interface?

            – genericname
            Dec 29 '18 at 10:21











          • Call downloadQuizzesFromCloud(this::print) and declare a function private void print(List<Quiz> quizzes) { // do everything here. }. If you're using JAVA-7 then do this following, downloadQuizzesFromCloud(this) and implement the onGet(List<Quiz> quizzes) function

            – Abdullah Aftab
            Dec 31 '18 at 4:27











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962633%2ffirestore-onsuccess-listener-isnt-working%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The onComplete is called when the read operation has completed from the Firestore servers. If it's not getting called, I can see two possible reasons:




          1. You're not connected to the server. Unless you've read the data before (and it's in the local database that the Firestore client maintains), this means the read never completes locally.

          2. You're not thinking asynchronously. Note that data is read from the server asynchronously, and there may be some time between when you call get() and when onComplete fires. To test if this is the case, put a breakpoint on if (task.isSuccessful()) { and run the app in the debugger. The breakpoint will hit when the data is read from the server.






          share|improve this answer
























          • Thank you, the problem is indeed because I am not thinking asynchronously. How can I make sure that objects I change in my code will change only after the server data read is complete?

            – genericname
            Dec 29 '18 at 8:35













          • You do that by calling the code from within onComplete, since that is the method that fires after the data has loaded.

            – Frank van Puffelen
            Dec 29 '18 at 15:11











          • this would work however if I were to use the variable "quizzes" somewhere else in my code, (for example just before the function "downloadQuizzesFromCloud" is finished) It is not promised that the task has finished and I could have incorrect data in "quizzes" variable. Is there a way to solve this?

            – genericname
            Dec 29 '18 at 15:16











          • Any code that depends on the quizes that are loaded asynchronously from the database should be either inside the onComplete (that is called when the data has loaded) or called from within that method. I commented the same on your previous question here: stackoverflow.com/questions/53970276/…

            – Frank van Puffelen
            Dec 29 '18 at 16:42


















          1














          The onComplete is called when the read operation has completed from the Firestore servers. If it's not getting called, I can see two possible reasons:




          1. You're not connected to the server. Unless you've read the data before (and it's in the local database that the Firestore client maintains), this means the read never completes locally.

          2. You're not thinking asynchronously. Note that data is read from the server asynchronously, and there may be some time between when you call get() and when onComplete fires. To test if this is the case, put a breakpoint on if (task.isSuccessful()) { and run the app in the debugger. The breakpoint will hit when the data is read from the server.






          share|improve this answer
























          • Thank you, the problem is indeed because I am not thinking asynchronously. How can I make sure that objects I change in my code will change only after the server data read is complete?

            – genericname
            Dec 29 '18 at 8:35













          • You do that by calling the code from within onComplete, since that is the method that fires after the data has loaded.

            – Frank van Puffelen
            Dec 29 '18 at 15:11











          • this would work however if I were to use the variable "quizzes" somewhere else in my code, (for example just before the function "downloadQuizzesFromCloud" is finished) It is not promised that the task has finished and I could have incorrect data in "quizzes" variable. Is there a way to solve this?

            – genericname
            Dec 29 '18 at 15:16











          • Any code that depends on the quizes that are loaded asynchronously from the database should be either inside the onComplete (that is called when the data has loaded) or called from within that method. I commented the same on your previous question here: stackoverflow.com/questions/53970276/…

            – Frank van Puffelen
            Dec 29 '18 at 16:42
















          1












          1








          1







          The onComplete is called when the read operation has completed from the Firestore servers. If it's not getting called, I can see two possible reasons:




          1. You're not connected to the server. Unless you've read the data before (and it's in the local database that the Firestore client maintains), this means the read never completes locally.

          2. You're not thinking asynchronously. Note that data is read from the server asynchronously, and there may be some time between when you call get() and when onComplete fires. To test if this is the case, put a breakpoint on if (task.isSuccessful()) { and run the app in the debugger. The breakpoint will hit when the data is read from the server.






          share|improve this answer













          The onComplete is called when the read operation has completed from the Firestore servers. If it's not getting called, I can see two possible reasons:




          1. You're not connected to the server. Unless you've read the data before (and it's in the local database that the Firestore client maintains), this means the read never completes locally.

          2. You're not thinking asynchronously. Note that data is read from the server asynchronously, and there may be some time between when you call get() and when onComplete fires. To test if this is the case, put a breakpoint on if (task.isSuccessful()) { and run the app in the debugger. The breakpoint will hit when the data is read from the server.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 29 '18 at 7:00









          Frank van PuffelenFrank van Puffelen

          229k28376400




          229k28376400













          • Thank you, the problem is indeed because I am not thinking asynchronously. How can I make sure that objects I change in my code will change only after the server data read is complete?

            – genericname
            Dec 29 '18 at 8:35













          • You do that by calling the code from within onComplete, since that is the method that fires after the data has loaded.

            – Frank van Puffelen
            Dec 29 '18 at 15:11











          • this would work however if I were to use the variable "quizzes" somewhere else in my code, (for example just before the function "downloadQuizzesFromCloud" is finished) It is not promised that the task has finished and I could have incorrect data in "quizzes" variable. Is there a way to solve this?

            – genericname
            Dec 29 '18 at 15:16











          • Any code that depends on the quizes that are loaded asynchronously from the database should be either inside the onComplete (that is called when the data has loaded) or called from within that method. I commented the same on your previous question here: stackoverflow.com/questions/53970276/…

            – Frank van Puffelen
            Dec 29 '18 at 16:42





















          • Thank you, the problem is indeed because I am not thinking asynchronously. How can I make sure that objects I change in my code will change only after the server data read is complete?

            – genericname
            Dec 29 '18 at 8:35













          • You do that by calling the code from within onComplete, since that is the method that fires after the data has loaded.

            – Frank van Puffelen
            Dec 29 '18 at 15:11











          • this would work however if I were to use the variable "quizzes" somewhere else in my code, (for example just before the function "downloadQuizzesFromCloud" is finished) It is not promised that the task has finished and I could have incorrect data in "quizzes" variable. Is there a way to solve this?

            – genericname
            Dec 29 '18 at 15:16











          • Any code that depends on the quizes that are loaded asynchronously from the database should be either inside the onComplete (that is called when the data has loaded) or called from within that method. I commented the same on your previous question here: stackoverflow.com/questions/53970276/…

            – Frank van Puffelen
            Dec 29 '18 at 16:42



















          Thank you, the problem is indeed because I am not thinking asynchronously. How can I make sure that objects I change in my code will change only after the server data read is complete?

          – genericname
          Dec 29 '18 at 8:35







          Thank you, the problem is indeed because I am not thinking asynchronously. How can I make sure that objects I change in my code will change only after the server data read is complete?

          – genericname
          Dec 29 '18 at 8:35















          You do that by calling the code from within onComplete, since that is the method that fires after the data has loaded.

          – Frank van Puffelen
          Dec 29 '18 at 15:11





          You do that by calling the code from within onComplete, since that is the method that fires after the data has loaded.

          – Frank van Puffelen
          Dec 29 '18 at 15:11













          this would work however if I were to use the variable "quizzes" somewhere else in my code, (for example just before the function "downloadQuizzesFromCloud" is finished) It is not promised that the task has finished and I could have incorrect data in "quizzes" variable. Is there a way to solve this?

          – genericname
          Dec 29 '18 at 15:16





          this would work however if I were to use the variable "quizzes" somewhere else in my code, (for example just before the function "downloadQuizzesFromCloud" is finished) It is not promised that the task has finished and I could have incorrect data in "quizzes" variable. Is there a way to solve this?

          – genericname
          Dec 29 '18 at 15:16













          Any code that depends on the quizes that are loaded asynchronously from the database should be either inside the onComplete (that is called when the data has loaded) or called from within that method. I commented the same on your previous question here: stackoverflow.com/questions/53970276/…

          – Frank van Puffelen
          Dec 29 '18 at 16:42







          Any code that depends on the quizes that are loaded asynchronously from the database should be either inside the onComplete (that is called when the data has loaded) or called from within that method. I commented the same on your previous question here: stackoverflow.com/questions/53970276/…

          – Frank van Puffelen
          Dec 29 '18 at 16:42















          0














          Use a callback interface. Just like this below.



          private void downloadQuizzesFromCloud(Consumer listener) {

          String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();

          final FirebaseFirestore db = FirebaseFirestore.getInstance();

          String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");

          Query userQuizzes = db.collection(user_quizzes_path);

          userQuizzes.get()
          .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
          @Override
          public void onComplete(@NonNull Task<QuerySnapshot> task) {
          if (task.isSuccessful()) {
          List<Quiz> quizzes = new ArrayList<>();
          for (QueryDocumentSnapshot document : task.getResult()) {
          Quiz quizDownloaded = getQuizFromCloud(document.getId());
          quizzes.add(quizDownloaded);
          }
          listener.onGet(quizzes);
          Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
          //TODO put in recycle adapter
          } else { }
          }
          });

          }
          interface Consumer {
          void onGet(List<Quiz> quizzes);
          }





          share|improve this answer
























          • I want to acheive something like this: downloadQuizzesFromCloud(); print(quizzes); and I want that quizzes will be updated when I print them. How does can I do this with a callback interface?

            – genericname
            Dec 29 '18 at 10:21











          • Call downloadQuizzesFromCloud(this::print) and declare a function private void print(List<Quiz> quizzes) { // do everything here. }. If you're using JAVA-7 then do this following, downloadQuizzesFromCloud(this) and implement the onGet(List<Quiz> quizzes) function

            – Abdullah Aftab
            Dec 31 '18 at 4:27
















          0














          Use a callback interface. Just like this below.



          private void downloadQuizzesFromCloud(Consumer listener) {

          String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();

          final FirebaseFirestore db = FirebaseFirestore.getInstance();

          String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");

          Query userQuizzes = db.collection(user_quizzes_path);

          userQuizzes.get()
          .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
          @Override
          public void onComplete(@NonNull Task<QuerySnapshot> task) {
          if (task.isSuccessful()) {
          List<Quiz> quizzes = new ArrayList<>();
          for (QueryDocumentSnapshot document : task.getResult()) {
          Quiz quizDownloaded = getQuizFromCloud(document.getId());
          quizzes.add(quizDownloaded);
          }
          listener.onGet(quizzes);
          Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
          //TODO put in recycle adapter
          } else { }
          }
          });

          }
          interface Consumer {
          void onGet(List<Quiz> quizzes);
          }





          share|improve this answer
























          • I want to acheive something like this: downloadQuizzesFromCloud(); print(quizzes); and I want that quizzes will be updated when I print them. How does can I do this with a callback interface?

            – genericname
            Dec 29 '18 at 10:21











          • Call downloadQuizzesFromCloud(this::print) and declare a function private void print(List<Quiz> quizzes) { // do everything here. }. If you're using JAVA-7 then do this following, downloadQuizzesFromCloud(this) and implement the onGet(List<Quiz> quizzes) function

            – Abdullah Aftab
            Dec 31 '18 at 4:27














          0












          0








          0







          Use a callback interface. Just like this below.



          private void downloadQuizzesFromCloud(Consumer listener) {

          String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();

          final FirebaseFirestore db = FirebaseFirestore.getInstance();

          String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");

          Query userQuizzes = db.collection(user_quizzes_path);

          userQuizzes.get()
          .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
          @Override
          public void onComplete(@NonNull Task<QuerySnapshot> task) {
          if (task.isSuccessful()) {
          List<Quiz> quizzes = new ArrayList<>();
          for (QueryDocumentSnapshot document : task.getResult()) {
          Quiz quizDownloaded = getQuizFromCloud(document.getId());
          quizzes.add(quizDownloaded);
          }
          listener.onGet(quizzes);
          Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
          //TODO put in recycle adapter
          } else { }
          }
          });

          }
          interface Consumer {
          void onGet(List<Quiz> quizzes);
          }





          share|improve this answer













          Use a callback interface. Just like this below.



          private void downloadQuizzesFromCloud(Consumer listener) {

          String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();

          final FirebaseFirestore db = FirebaseFirestore.getInstance();

          String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");

          Query userQuizzes = db.collection(user_quizzes_path);

          userQuizzes.get()
          .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
          @Override
          public void onComplete(@NonNull Task<QuerySnapshot> task) {
          if (task.isSuccessful()) {
          List<Quiz> quizzes = new ArrayList<>();
          for (QueryDocumentSnapshot document : task.getResult()) {
          Quiz quizDownloaded = getQuizFromCloud(document.getId());
          quizzes.add(quizDownloaded);
          }
          listener.onGet(quizzes);
          Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
          //TODO put in recycle adapter
          } else { }
          }
          });

          }
          interface Consumer {
          void onGet(List<Quiz> quizzes);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 29 '18 at 10:07









          Abdullah AftabAbdullah Aftab

          3214




          3214













          • I want to acheive something like this: downloadQuizzesFromCloud(); print(quizzes); and I want that quizzes will be updated when I print them. How does can I do this with a callback interface?

            – genericname
            Dec 29 '18 at 10:21











          • Call downloadQuizzesFromCloud(this::print) and declare a function private void print(List<Quiz> quizzes) { // do everything here. }. If you're using JAVA-7 then do this following, downloadQuizzesFromCloud(this) and implement the onGet(List<Quiz> quizzes) function

            – Abdullah Aftab
            Dec 31 '18 at 4:27



















          • I want to acheive something like this: downloadQuizzesFromCloud(); print(quizzes); and I want that quizzes will be updated when I print them. How does can I do this with a callback interface?

            – genericname
            Dec 29 '18 at 10:21











          • Call downloadQuizzesFromCloud(this::print) and declare a function private void print(List<Quiz> quizzes) { // do everything here. }. If you're using JAVA-7 then do this following, downloadQuizzesFromCloud(this) and implement the onGet(List<Quiz> quizzes) function

            – Abdullah Aftab
            Dec 31 '18 at 4:27

















          I want to acheive something like this: downloadQuizzesFromCloud(); print(quizzes); and I want that quizzes will be updated when I print them. How does can I do this with a callback interface?

          – genericname
          Dec 29 '18 at 10:21





          I want to acheive something like this: downloadQuizzesFromCloud(); print(quizzes); and I want that quizzes will be updated when I print them. How does can I do this with a callback interface?

          – genericname
          Dec 29 '18 at 10:21













          Call downloadQuizzesFromCloud(this::print) and declare a function private void print(List<Quiz> quizzes) { // do everything here. }. If you're using JAVA-7 then do this following, downloadQuizzesFromCloud(this) and implement the onGet(List<Quiz> quizzes) function

          – Abdullah Aftab
          Dec 31 '18 at 4:27





          Call downloadQuizzesFromCloud(this::print) and declare a function private void print(List<Quiz> quizzes) { // do everything here. }. If you're using JAVA-7 then do this following, downloadQuizzesFromCloud(this) and implement the onGet(List<Quiz> quizzes) function

          – Abdullah Aftab
          Dec 31 '18 at 4:27


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962633%2ffirestore-onsuccess-listener-isnt-working%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas