2

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;

}
4
  • Java doesn't have global variables.
    – shmosel
    Feb 9, 2017 at 5:28
  • my bad. have made the change .. any help would be great @shmosel Feb 9, 2017 at 5:30
  • Create Your Variable static . Feb 9, 2017 at 5:31
  • 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 called when the data is received from server. So you should put all function calls that you require after check inside listener. Feb 9, 2017 at 5:31

2 Answers 2

7

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
       }
   }
});
2
  • Thank you @Grisgram May 7, 2017 at 1:46
  • this is a brilliant solution. I've spent weeks trying to handle async calls in Firebase and this is just perfect! 👌 Jan 26, 2020 at 22:20
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.