Gastón Saillén
Gastón Saillén

Reputation: 13129

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 exists , here is what i have:

mDatabase.child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("URL_CARS").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(final DataSnapshot dataSnapshotCars) {
                mDatabase.child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("URL_PLANES").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshotPlanes) {

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

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

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

Database Structure

Users +_
        -L4K1JDJS4K2599SK3+_
                            URL_CARS: 
                            URL_PLANES:

Upvotes: 1

Views: 1398

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

To solve this, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(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();
        }   
    }

    @Override
    public void onCancelled(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")()) {}

Upvotes: 1

Rosário P. Fernandes
Rosário P. Fernandes

Reputation: 11326

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:

@Override
                    public void onDataChange(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:

@Override
                    public void onDataChange(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();
                        }
                    }

Upvotes: 2

Related Questions