简体   繁体   English

如何将值与 Firebase 实时数据库的子项进行比较?

[英]How to compare value with child of Firebase Realtime Database?

I would to compare a value with a child of my Firebase Realtime Database but I don't know how to do.我想将一个值与我的 Firebase 实时数据库的孩子进行比较,但我不知道该怎么做。 The structure of my database is:我的数据库结构是:

在此处输入图像描述

This is the code that I wrote:这是我写的代码:

email = loadPreferences();
mAuth = FirebaseAuth.getInstance();

FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference myRef = database.getReference().child("users");
        myRef.orderByChild("email").equalTo(email).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.getValue() != null) {
                    //loop through the keys
                    for(DataSnapshot datasnap : snapshot.getChildren()) {

                        if(!email.equalsIgnoreCase("")) {
                            myRef.child("users").child("email").child("address").setValue(getAddress());
                        }
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });

I tried with debugger but when it arrived at "orderByChild()" it skip all and jumps to end.我尝试使用调试器,但是当它到达“orderByChild()”时,它会跳过所有并跳转到结束。 Anyone can help me?任何人都可以帮助我吗? Thanks in advance提前致谢

Data is loaded from Firebase asynchronously, because it needs to be loaded from the cloud.数据从 Firebase 异步加载,因为需要从云端加载。 Instead of blocking the app for your users, Firebase instead loads the data in the background and lets your main code continue, which is what you see happening when you step through the code. Firebase 不是为您的用户阻止应用程序,而是在后台加载数据并让您的主代码继续执行,这就是您在单步执行代码时看到的情况。

Instead of stepping through the code, place a breakpoint on the first line inside onDataChange , and allow the code to run.不要单步执行代码,而是在onDataChange内的第一行放置一个断点,并允许代码运行。 Then when the data is available, the debugger will hit your breakpoint and you can continue debugging.然后当数据可用时,调试器将命中您的断点,您可以继续调试。

You should also implement onCancelled , as you're now ignoring possible problems.您还应该实施onCancelled ,因为您现在忽略了可能的问题。 At its minimum, this method should be:这种方法至少应该是:

public void onCancelled(@NonNull DatabaseError databaseError) { 
  throw databaseError.toException(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM