如何知道firebase ChildEventListener是否发现孩子? [机器人] [英] how to know if firebase ChildEventListener found child ? [android]

查看:119
本文介绍了如何知道firebase ChildEventListener是否发现孩子? [机器人]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图通过他们的电子邮件获取用户密钥,问题是,我不知道我的代码里面,如果查询实际上发现的东西或没有..所以我假设,如果我在onchildadded,查询是成功的,一个孩子已被发现,所以我会通过钥匙到另一个活动,并停止当前的活动,但是当我运行应用程序的整个代码得到执行..我觉得我的方式有点不对,但我没有找到任何方式知道如果查询是sucsessful或孩子被发现....如果您有任何想法请求帮助...

  public void searchemail(String email){

Firebase ref = new Firebase(https://< myfirebase> .firebaseio.com / users);
Query queryRef = ref.orderByChild(Email)。equalTo(email);

$ b ChildEventListener listener = new ChildEventListener(){
$ b @Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){

userkey = dataSnapshot.getKey();
homeintent.putExtra(key,userkey);
startActivity(homeintent);
finish();
return;
//我想让代码停在这里

}}
queryRef.addChildEventListener(listener);

Toast Toast = Toast.makeText(这个找不到电子邮件,Toast.LENGTH_SHORT); / /我假设如果我在这里然后没有找到孩子,但总是得到执行之前,开始活动
}



输出:
如果找到电子邮件 - > toast将显示,然后家庭活动将开始...
如果没有找到电子邮件 - >只有烤面包将显示..


<当发生相关的事件时, ChildEventListener 的方法被调用 所以 onChildAdded()添加子项时将被称为。出于这个原因,如果小孩存在,你不能轻易使用 ChildEventListener 来检测

检测孩子是否存在的最简单方法是使用 ValueEventListener

  public void searchemail(String email){

Firebase ref = new Firebase(https://< myfirebase> .firebaseio.com / users);
Query queryRef = ref.orderByChild(Email)。equalTo(email);

$ b ValueEventListener listener = new ValueEventListener(){
$ b @Override
public void onDataChanged(DataSnapshot snapshot){
if(snapshot。 exists()){
for(DataSnapshot child:snapshot.getChildren()){
homeintent.putExtra(key,child.getKey());
startActivity(homeintent);
break; //退出for循环,我们只想要一个匹配

$ b $ else $ $ $ $ toast.makeText Toast.makeText(这个找不到电子邮件,Toast.LENGTH_SHORT) ;
}
}
};
queryRef.addValueEventListener(listener);
}


so I'm trying to get users key by their email and the problem is that I dont know inside my code if the query actually found something or not .. so I'm assuming if I'm inside onchildadded that the query is successful and a child has been found so I will pass the key to another activity and stop current activity but when I run the app the whole code get executed .. I feel my way is kinda wrong but I didn't find any way to know if the query is sucsessful or a child is found .... if you have any idea pleas help ...

  public void searchemail(String email){

  Firebase ref = new Firebase("https://<myfirebase>.firebaseio.com/users");
    Query queryRef = ref.orderByChild("Email").equalTo(email);


     ChildEventListener listener = new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            userkey = dataSnapshot.getKey();
              homeintent.putExtra("key", userkey);
               startActivity(homeintent);
               finish();
               return; 
                 // I want code to stop here 

        }}
    queryRef.addChildEventListener(listener);

      Toast toast = Toast.makeText(this, "email not found", Toast.LENGTH_SHORT); // Im assuming if Im here then no child is found but this always get executed before startactivity  
   }

OUTPUT : if email found -> toast will show then home activity will start... if email not found -> only toast will show..

解决方案

The methods of a ChildEventListener get called when the relevant event happened. So onChildAdded() will be called when a child has been added. For this reason you cannot easily use a ChildEventListener to detect if a child exists.

The easiest way to detect if a child exists, is to use a ValueEventListener:

public void searchemail(String email){

  Firebase ref = new Firebase("https://<myfirebase>.firebaseio.com/users");
  Query queryRef = ref.orderByChild("Email").equalTo(email);


   ValueEventListener listener = new ValueEventListener() {

     @Override
     public void onDataChanged(DataSnapshot snapshot) {
       if (snapshot.exists()) {
         for (DataSnapshot child: snapshot.getChildren()) {
           homeintent.putExtra("key", child.getKey());
           startActivity(homeintent);
           break; // exit for loop, we only want one match
         }
       }
       else {
         Toast toast = Toast.makeText(this, "email not found", Toast.LENGTH_SHORT);
       }
     }
   };
   queryRef.addValueEventListener(listener);
}

这篇关于如何知道firebase ChildEventListener是否发现孩子? [机器人]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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