Firebase always true when checking if data exists

时间秒杀一切 提交于 2019-12-24 09:36:19

问题


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:

回答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:

@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();
                        }
                    }



回答2:


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")()) {}


来源:https://stackoverflow.com/questions/48190268/firebase-always-true-when-checking-if-data-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!