Reputation: 7759
I'm trying to check if a user with a particular email exists or not. The user with this email is in the database so the check should have returned true instead of false.
Here's the schema:
Here's the code:
final DatabaseReference userRef = FirebaseDatabase.getInstance().getReference().child("users");
userRef.orderByChild("email").equalTo("[email protected]").limitToFirst(1).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println(dataSnapshot); //returns { key = users, value = null }
if(dataSnapshot.getChildrenCount() > 0) {
//user already exists (Never executed)
} else {
//user doesn't exists (Always executed)
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 4
Views: 1320
Reputation: 10330
If setPersistenceEnabled(true)
is called, use of addListenerForSingleValueEvent
can cause local cached version of data to be returned rather than latest version from server (whereas calling addValueEventListener
will ensure you get latest version). Another approach, if using addListenerForSingleValueEvent
is to call query.keepSynced(true);
Upvotes: 3