0

FirebaseDatabase.getInstance().getReference("Block").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snap : dataSnapshot.getChildren()) {
            if(snap.getKey().equals(myUID)){
                FirebaseDatabase.getInstance().getReference("Block").child(myUID).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot snap : dataSnapshot.getChildren()) {
                            if(snap.getKey().equals(list.get(position).getUid())){
                                FirebaseDatabase.getInstance().getReference("Block").child(myUID).child(list.get(position).getUid()).addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        for (DataSnapshot snap : dataSnapshot.getChildren()) {
                                            if(snap.getValue().equals("1")){
                                               call_method();
                                            }else{

                                              //code...
                                            }
                                        }
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });

                            } else {
                               call_method();
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            } else {
               call_method();
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

This code works well, but it is very long so I am asking for the best way to write it ?

I want to check firstly if Block node has child equals to my UID if it is true then I want to check if my UID has child equals to specific UID if it is true then I want to get the value of block which is 0 in this example

How can I do that ? Thank you very much.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Islam Ahmed
  • 668
  • 9
  • 19
  • If you read the location `Block`, the snapshot is going to contain everything under that location. You don't need to re-query for each child. Just reach into the snapshot children to find the data you're looking for. https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot – Doug Stevenson Mar 10 '18 at 19:11
  • Thanks, I read the document but I don't know How can I read the location .. sorry I am a beginner in Firebase ..Can you help me ? – Islam Ahmed Mar 10 '18 at 19:44
  • Use the methods the refer to "child" or "children". – Doug Stevenson Mar 10 '18 at 19:48
  • 3
    Possible duplicate of [Checking if a particular value exists in the firebase database](https://stackoverflow.com/questions/47893328/checking-if-a-particular-value-exists-in-the-firebase-database) – Alex Mamo Mar 12 '18 at 10:27

2 Answers2

2

Just traverse to the path specified using child. You will get no data if the path doesn't exist.

FirebaseDatabase.getInstance().getReference("Block")
    .child("MyUUID")
    .child("specificUUID")
    .addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()) {
                // ... Do something
            } else {
                call_method();
            }
       // ... Other code
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Thanks , but the problem here in this code is that there is a method I want to call when child doesn't exist `call_method();` , How can I call it ? – Islam Ahmed Mar 10 '18 at 20:24
  • Great to know that helped! The answer is edited based on your suggestion as well. Thanks. – Reaz Murshed Mar 11 '18 at 06:40
1

Try this:

Firebaseuser user=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Block");
ref.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
       if(dataSnapshot.exist()){
          //checks first uid
           if(dataSnapshot.hasChild(seconduid){
               //checks if seconduid is there
                 for(DataSnapshot datas:dataSnapshot.getChildren()){
                      String blocks=datas.child("block").getValue().toString();

          }
      }

   }  

}

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

This will check the first uid if(dataSnapshot.exist()) after that it will check if that uid node has the second uid that you provide if(dataSnapshot.hasChild(seconduid){ then you will iterate and get the node block

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134