Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value from valueEventListener java

I am trying to return a boolean but the value returned is always false, i.e. 0 in this case.

The check variable is an instance variable and the below function is getting called when a button is pressed.

private boolean checkAnswerSubmission() {

    DatabaseReference answerDatabase = FirebaseDatabase.getInstance().getReference().child("userPuzzleHistory").child(uid);
    answerDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.hasChild(String.valueOf(imagename))) {
                check = 1;
            } else {
                check = 0;
            }
     //here the value check is working fine but the value cannot be returned from here.
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    //here the value returned is always 0
    if (check == 1)
        return true;
    else
        return false;

}
like image 543
Saransh Agarwal Avatar asked Jan 16 '26 22:01

Saransh Agarwal


2 Answers

As with all async operations I often do it in a callback manner.

Your solution could work like this:

  1. Create a simple callback interface (I have mine in a library that I use in almost every app)

    public interface SimpleCallback {
        void callback(Object data);
    }
    
    // You could do it as well generic, that's what I do in my lib:
    public interface SimpleCallback<T> {
        void callback(T data);
    }
    

Then redesign the signature of your method like this:

private void checkAnswerSubmission(@NonNull SimpleCallback<boolean> finishedCallback) {

    DatabaseReference answerDatabase = FirebaseDatabase.getInstance().getReference().child("userPuzzleHistory").child(uid);
    answerDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This will simple call your callback containing a boolean true/false
            finishedCallback.callback(dataSnapshot.hasChild(String.valueOf(imagename)));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

You call this with:

checkAnswerSubmission(new SimpleCallback<boolean>() {
   @Override
   void callback(boolean data) {
       if (data) {
          // true was returned
       } else {
          // false was returned
       }
   }
});
like image 56
Grisgram Avatar answered Jan 19 '26 20:01

Grisgram


The database reference is not done on the same thread as that running your function. It just adds a listener for the async call that is only made when the data is received from server.

So your program initializes a listener and moves on to the next statement in this line.

answerDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.hasChild(String.valueOf(imagename))) {
                check = 1;
            } else {
                check = 0;
            }
     //here the value check is working fine but the value cannot be returned from here.
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Now as the program doesn't wait for response to be received from the server it executes :

if (check == 1)
        return true;
    else
        return false;

So if your check value was initialized it will most likely take 0 as value and always return false.

like image 29
Sourav Kanta Avatar answered Jan 19 '26 20:01

Sourav Kanta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!