0
 for (int i = 1; i < 3; i++) {
        list.add(new Integer(i));
    }
    Collections.shuffle(list);

    for (int i = 0; i < 2; i++) {

        int r = list.get(i);
        numeroRandom = Integer.toString(r);
        Log.d("DEBUG", "teste random :" + list.get(i));
        Log.d("DEBUG", "numeroRandomGerado = " + numeroRandom);

        final DatabaseReference questaoRef = fireBaseRef.child("questoes");
        Log.d("DEBUG", "Loop Stop here");
        questaoRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Log.d("DEBUG", "teste2");
                if (dataSnapshot != null) {
                    Questoes questoes = dataSnapshot.child("id").child(numeroRandom).getValue(Questoes.class);


                    Log.d("DEBUG", "teste3");
                    Questoes objetoQuestao = new Questoes();
                    objetoQuestao.setQuestao(questoes.getQuestao());
                    objetoQuestao.setOpcaoA(questoes.getOpcaoA());
                    objetoQuestao.setOpcaoB(questoes.getOpcaoB());
                    objetoQuestao.setOpcaoC(questoes.getOpcaoC());
                    objetoQuestao.setOpcaoD(questoes.getOpcaoD());
                    objetoQuestao.setResultado(questoes.getResultado());
                    listaQuestoes.add(objetoQuestao);
                    Log.d("DEBUG", "tamanho = " + listaQuestoes.size());


                }
                callBack.onCallback(listaQuestoes);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException();
            }
        });

For some reason my loop, stops on Log.d("DEBUG", "Loop Stop here") and back to the start and execute 2 times the rest of code.

this is the Log i get from the code, for some reason stops on the 3° debug and restarts and execute everything all over

teste random :1
numeroRandomGerado = 1
Loop stop here
teste random :2
numeroRandomGerado = 2
Loop stop here - teste 1
teste2
tamanho = 1
callback
teste2
teste3

When the correct would be like this:

teste random :1
numeroRandomGerado = 1
Loop stop here - teste 1
teste2
teste3
tamanho = 1
callBack
teste random :2
numeroRandomGerado = 2
Loop stop here
teste2
teste3
tamanho = 2
callBack
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

this is because the onDataChange(DataSnapshot dataSnapshot) listener is where the execution becomes asynchronous - while you expect synchronous execution. possibly even because it might have another listener registered for .child("questoes"), while the previously registered listener has not yet returned. try setting breakpoints and step into it, to see in which order the execution actually happens. adding Thread.sleep(2000); after registering the listener, most likely should make it work, as you'd expect it to work.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • there's only one listener in the onCreate, i forgot to add it on the code above, when i arrive home i will test the Thread.sleep and check with a breakpoint the code, thanks for the help! – Lucas Gomes Sep 18 '18 at 23:29
  • @LucasGomes I've meant that `ValueEventListener` in the `for` loop... which might not be given enough time, during the first one iteration of the loop - and shortly after bound again, during the second iteration of the loop. this is because the `for` loop executes synchronously, while the listener executes asynchronously. – Martin Zeitler Sep 18 '18 at 23:47
  • I used the breakpoint to test and see the order like you said and for some reason the code insta back to start before the listener is add on the first loop. I created other activity and everything is runnig fine now, Thanks for the help – Lucas Gomes Sep 19 '18 at 05:03
  • @LucasGomes Trying to delay the execution by calling sleep method is not a good idea. It might solve the problem partial, but if the time to get the data from the database is bigger than 2 seconds? To solve this this issue I recommend you to wait for the data using a custom callback. For that, please see the last part of my answer from this **[post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)**. – Alex Mamo Sep 19 '18 at 07:09
  • @AlexMamo only suggested that, to show why the loop progresses unexpectedly. the `Tasks API` might be way more reliable, than to use an estimated delay. https://developers.google.com/android/guides/tasks – Martin Zeitler Sep 19 '18 at 09:29
  • @MartinZeitler The `Tasks API` is also an option. I recommend it too. – Alex Mamo Sep 19 '18 at 09:34
  • 1
    @AlexMamo in this case, that would be `Task.continueWithTask` instead of a `for` loop. – Martin Zeitler Sep 19 '18 at 09:35