当检查数据是否存在时,Firebase始终为true [英] Firebase always true when checking if data exists

查看:50
本文介绍了当检查数据是否存在时,Firebase始终为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查当前2个引用处的数据是否存在,但似乎它总是返回true(总是第一个Toast),我想这不是在检查URL_CARS和URL_PLANES是否存在,这就是我所拥有的:

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) {}
                });

数据库结构

Users +_
        -L4K1JDJS4K2599SK3+_
                            URL_CARS: 
                            URL_PLANES:

推荐答案

在尝试读取数据时,Firebase始终会为您返回快照,并且快照永远不会为null.因为 Datasnapshot 对象由两个属性组成: key value ,所以它类似于 Datasnapshot(key,value).当您基于某个键查询数据库时,该值可能不存在(您的情况),但是您已将该键传递给了该Datasnapshot对象(这意味着该键存在).因此,您获得的Datasnapshot类似于: Datasnapshot(key ="URL_PLANES",value = null).这意味着 datasnapshot!= null 将始终为真.

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.

您可以做的是通过使用 exists()方法检查值是否存在:

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

这篇关于当检查数据是否存在时,Firebase始终为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆