2
votes

This is my data on Firebase-

app-<br />
-Users<br />
 +366MWex62IhUaOwbkjjGedIgLQJ3<br />
 -sfsfaegaergg<br />
     firstname: "John"<br />
     lastname: "Doe"<br />
     phone: "+1123456789"<br />

There are two users in my db for now. My input is phone = "+1123456789" (2nd user)<br />

This is my code-


    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference rf = rootRef.child("Users");
        com.google.firebase.database.Query query = rf.orderByChild("phone").equalTo("+1123456789");
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot snap: dataSnapshot.getChildren()) {
                    Log.v("children", snap.getChildren().toString());
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
    });

I am not getting the details of the second user. The debugger goes till addValueListener and then directly exits. Where do I need to make corrections? Thank you.

3
"The debugger goes till addValueListener and then directly exits" - the result will come back asynchronously. Have you set breakpoint in onDataChange?John O'Reilly
Yes. It comes back after a while. This is normal right?Kedar
@Kedar yes, that's how it work. So is your problem solved?koceeng
It solved the problem but I don't understand the reason why it is taking so long to read data suddenly. I have to wait for one complete minute for the firebase to get data. :/Kedar

3 Answers

1
votes

"The debugger goes till addValueListener and then directly exits" - the result will come back asynchronously. Have you set breakpoint in onDataChange? – John O'Reilly

Thank you.

0
votes
 mFirebaseRef = mFirebaseInstance.getReference();
    DatabaseReference queryLocation = 
 mFirebaseRef.child("BloodGroupDetail");
    Query query = queryLocation.orderByChild("bloodgroup").equalTo("B+");
    query.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            getUpdate(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


    });
0
votes

Try this :

 DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("Users");    
rootRef.orderByChild("phone").equalTo("+1123456789").addListenerForSingleValueEvent(
    new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //data will be available on dataSnapshot.getValue();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
        }
});