如何从onDataChange方法传递数据?

时间:2018-09-15 20:12:09

标签: java android firebase firebase-realtime-database

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

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

1 个答案:

答案 0 :(得分:2)

如果您尝试执行此类操作,则由于Firebase是异步的,因此始终会返回false,因此您需要根据连接情况稍等一下,才能从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()的结果时立即触发,然后您可以从那里返回任何内容

首先创建一个界面

public interface FirebaseSuccessListener {

    void onDataFound(boolean isDataFetched);

}

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

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

内部获取您的值
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