0

I make an application on android for my school but I block on a problem of asynchrony :

databaseReference2.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(final DataSnapshot ds: dataSnapshot.getChildren()){
                Log.e("test","1");
                Menu = ds.getValue(menu.class);
                list2.add(Menu.getId().toString());

                databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        try{
                            Log.e("test","2");
                            if(dataSnapshot.child("menuAjoute").child(mail).exists()){
                                    if(dataSnapshot.child("menuAjoute").child(mail).child(Menu.getId()).getValue().equals(Menu.getId())) {
                                        list.add(Menu.getNom()); 
                                    }
                            }
                        }catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
                Log.e("test","3");
            }

For my code to work, step 2 must be before step 3. But I have not found a way to do it. What would be the simplest way? (I searched for the solution but none of it worked).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

0

Firebase listener is asynchronous, so that means it won't wait until the data is extracted, which means that will keep executing the following lines of codes and at some point it will extract data from the database. What I will suggest to you is to put your Step 3 inside of the listener

try{
                            Log.e("test","2");


    if(dataSnapshot.child("menuAjoute").child(mail).exists()){
                                    if(dataSnapshot.child("menuAjoute").child(mail).child(Menu.getId()).getValue().equals(Menu.getId())) {
                                        list.add(Menu.getNom()); 
                                    }
                            }
                        }catch (Exception e) {
                            e.printStackTrace();
                        }

Log.e("test", "3")
Yunus Kulyyev
  • 1,022
  • 15
  • 26
0

There is no good way to force a wait in Android. If you want step 2 to be finished before step 3, you should move step 2 into the method that is called when step 2 completes. So:

databaseReference2.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(final DataSnapshot ds: dataSnapshot.getChildren()){
            Log.e("test","1");
            Menu = ds.getValue(menu.class);
            list2.add(Menu.getId().toString());

            databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    try{
                        Log.e("test","2");
                        if(dataSnapshot.child("menuAjoute").child(mail).exists()){
                                if(dataSnapshot.child("menuAjoute").child(mail).child(Menu.getId()).getValue().equals(Menu.getId())) {
                                    list.add(Menu.getNom()); 
                                }
                        }
                        Log.e("test","3");
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                    throw databaseError.toException();
                }
            });
        }

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for helping me. It's a stupid mistake x), but like that I will not do it anymore. Thanks for taking time to respond to me – Damien Sobredo Mar 25 '18 at 09:10