Skip to content Skip to sidebar Skip to footer

Firebase Always True When Checking If Data Exists

I want to check if the data at the current 2 references exists, but it seems it returns always true (always the first Toast ) and i think is not checking if URL_CARS AND URL_PLANES

Solution 1:

Firebase will always return you a snapshot when you're trying to read data and it won't ever be null. Because a Datasnapshot object consists of two attributes: key and value, so it's something like Datasnapshot(key, value). When you query your database based on a certain key, the value might not exist (your case) but you have passed the key to that Datasnapshot object (which means the key exists). So the Datasnapshot that you're getting in return is something like: Datasnapshot(key="URL_PLANES", value=null). Which means datasnapshot!=null will always true.

What you could do instead, is checking if value exists, by using the exists() method:

@OverridepublicvoidonDataChange(DataSnapshot dataSnapshotPlanes) {

                        if (dataSnapshotCars.exists() && dataSnapshotPlanes.exists()) {
                            Toast.makeText(LoginActivity.this, "DATA EXISTS", Toast.LENGTH_SHORT).show();

                        }else{
                            Toast.makeText(LoginActivity.this, "Data DOSNT EXISTS", Toast.LENGTH_SHORT).show();
                        }
                    }

Or get the snapshot value and then check if it isn't null:

@OverridepublicvoidonDataChange(DataSnapshot dataSnapshotPlanes) {

                        if (dataSnapshotCars.getValue()!=null && dataSnapshotPlanes.getValue()!=null) {
                            Toast.makeText(LoginActivity.this, "DATA EXISTS", Toast.LENGTH_SHORT).show();

                        }else{
                            Toast.makeText(LoginActivity.this, "Data DOSNT EXISTS", Toast.LENGTH_SHORT).show();
                        }
                    }

Solution 2:

To solve this, please use the following code:

Stringuid= FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReferencerootRef= FirebaseDatabase.getInstance().getReference();
DatabaseReferenceuidRef= rootRef.child("Users").child(uid);
ValueEventListenereventListener=newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.child("URL_CARS").exists()) {
            Toast.makeText(LoginActivity.this, "DATA EXISTS", Toast.LENGTH_SHORT).show();
        }
        if(dataSnapshot.child("URL_PLANES").exists()) {
            Toast.makeText(LoginActivity.this, "DATA EXISTS", Toast.LENGTH_SHORT).show();
        }   
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);

As you probably see i have used exists() method, which returns true if the snapshot contains a non-null value.

There is also another approach which sounds like this:

if(dataSnapshot.hasChild("URL_CARS")()) {}
if(dataSnapshot.hasChild("URL_PLANES")()) {}

Post a Comment for "Firebase Always True When Checking If Data Exists"