获取监听器内部变量的值

huangapple go评论99阅读模式
英文:

Get value of variable inside a listener

问题

I'm checking if an username is repeated or not in my firebase database. When the username exists, the value of usernameExist inside the method onDataChanged() would be true, but when I try to access the same variable outside the method onDataChanged(), the value is always false.

public boolean checkRepeatedUsername(){
    refUsernames = FirebaseDatabase.getInstance().getReference();
    refUsernames.child(username.getEditText().getText().toString().trim());
    refUsernames.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.getValue() != null){
                usernameExists = true;
                System.out.println("Exists username? " + " " + usernameExists); // this prints true;
            }
        }

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

        }
    });

    System.out.println("Exists? " + " " + usernameExists); // but this always prints false

    if(usernameExists){
        return true;
    }else{
        return false;
    }
}

请注意,这段代码中存在异步问题,onDataChanged() 是在后台线程中执行的,因此在该方法之后立即检查 usernameExists 的值会导致它总是为 false。您可能需要在 onDataChanged() 中处理结果或使用回调等方式来确保在数据准备好后再进行操作。

英文:

Im checking if an username is repeated or not in my firebase database, when username exists, the value of
usernameExist inside the method onDataChanged() would be true, but when I try to access, to the same variable outside the method onDataChanged(), ever the value is false.

 public boolean checkRepeatedUsername(){
        refUsernames = FirebaseDatabase.getInstance().getReference();
        refUsernames.child(username.getEditText().getText().toString().trim());
        refUsernames.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.getValue() != null){
                    usernameExists = true;
                    System.out.println("Exists username? " + " " + usernameExists); //this print true;
                }

            }

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

            }
        });

        System.out.println("Exists? " + " " + usernameExists); //but this ever print false

        if(usernameExists){
            return true;
        }else{
            return false;
        }
}

答案1

得分: 0

addListenerForSingleValueEvent 是异步的,在任何数据可用之前就立即返回。提供的回调将在数据可用时被调用。

由于 addListenerForSingleValueEvent 立即返回,因此你的打印 usernameExists 的代码行始终会打印其初始值。

只有在回调中数据可用时才能使用快照中的数据。你不能像现在这样同步使用它 - 你需要重写你的代码来接受异步行为。

英文:

addListenerForSingleValueEvent is asynchronous and returns immediately, before any data is available. The provided callback will be invoked whenever data becomes available.

Since addListenerForSingleValueEvent returns immediately, your line of code that prints usernameExists is always going to print its initial value.

You can only use the data in the snapshot when it becomes available in the callback. You can't use it synchronously as you are now - you will have to rewrite your code to accept the asynchronous behavior.

huangapple
  • 本文由 发表于 2020年7月31日 03:16:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63179881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定