如何从onDataChange方法传递数据? [英] How do I pass data out of the onDataChange method?

查看:54
本文介绍了如何从onDataChange方法传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个布尔值来检查应用程序中是否有重复的用户名.我希望布尔值基于ValueEventListener中的onDataChange的结果返回数据.这就是我所拥有的:

I have a boolean set up to check for duplicate usernames in an app. I want the boolean to return data based on the result of an onDataChange in a ValueEventListener. Here's what I have:

private boolean isUsernameValid(final String username) {


    mReference = FirebaseDatabase.getInstance().getReference();
    Query query = mReference.child("users").child(username);
    ValueEventListener mListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                //create condition that would make isUsernameValid return false

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };

    mReference.addListenerForSingleValueEvent(mListener);

    return //if that condition is not met;
}

推荐答案

如果尝试执行此类操作,则将始终返回false,因为Firebase是异步的,因此您需要稍等片刻,具体取决于您的连接为了从 dataSnapshot()

If you try to do something like this, you will always be returning false, since Firebase is asynchronous, you will need to wait a little bit depending on your connection in order to return any value from dataSnapshot()

private boolean isUsernameValid(final String username) {

        boolean isUsernameValid;
        mReference = FirebaseDatabase.getInstance().getReference();
        Query query = mReference.child("users").child(username);
        ValueEventListener mListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {

                    //create condition that would make isUsernameValid return false
                     isUsernameValid = true;
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };

        mReference.addListenerForSingleValueEvent(mListener);

        return isUsernameValid //if that condition is not met;
    }

要解决此问题,您可以创建一个接口,该接口将在我们知道我们从 onDataChange()获得结果后立即触发,然后您可以从那里返回任何内容

To solve this issue you can create an interface that will fires right when we know we have the results from onDataChange() and then you can return anything from there

首先创建一个界面

public interface FirebaseSuccessListener {

    void onDataFound(boolean isDataFetched);

}

然后我们只执行您的方法来检查用户是否存在

Then we just do your method that checks for the user if exists

private void isUsernameValid(final String username, FirebaseSuccessListener dataFetched) {


            mReference = FirebaseDatabase.getInstance().getReference();
            Query query = mReference.child("users").child(username);
            ValueEventListener mListener = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {

                        //here we are telling to our interface that the data has been found and then we can do anything with it outside this method
                         dataFetched.onDataFound(true);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            };

            mReference.addListenerForSingleValueEvent(mListener);


        }

然后我们可以调用该方法以便从 onDataChange()

And then we can call the method in order to get your value from inside onDataChange()

isUsernameValid.(new FirebaseSuccessListener() {
    @Override
    public void onCallback(boolean isDataFetched) {
        if(isDataFetched){
           //you know the value is true, here you can update or request any change for example you can do this 
            userIsOnFirebase(true);

      }else{
      userIsOnFirebase(false);
      }
    }
});


private boolean userIsOnFirebase(boolean isOnFirebase){
return isOnFirebase;
}

然后使用上面的此方法返回用户是否在您的数据库中.请确保在调用 userIsOnFirebase 时,因为如果仍未获取数据,它将返回false

then use this method above to return if the user is or not in your db. Make sure when you call userIsOnFirebase since it will return false if the data is still not fetched

这篇关于如何从onDataChange方法传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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