1

I am calling this class' method from other class and i always gets false, How can I update the outer scope data member (status) in

onSuccess()

method, whenever I call this method it always return null, can anyone tell me how can I get the correct result

public class UserAccount {

Firebase firebase;

private UserAccount() {
firebase = new Firebase(firebase_url);
}

public static UserAccount getUserAccountInstance(){
    return userAccountInstance;
}



boolean status = false; 


   public boolean createUserAccount(String username, String password)throws FirebaseException{

    if(firebase == null)
        firebase = new Firebase(firebase_url);
    firebase.createUser(username+"@firebase.com", password, new Firebase.ValueResultHandler<Map<String, Object>>() {

        @Override
        public void onSuccess(Map<String, Object> result) {
            System.out.println("Successfully created user account with uid:" + result.get("uid"));
            UserAccount.this.status=true;

        }

        @Override
        public void onError(FirebaseError firebaseError) {
            System.out.println("Something went wrong!! \n Account cannot be created.. useraccount");
            throw new FirebaseException(firebaseError.getMessage());

        }
    });

    System.out.println("user"+status);// this produces false

    return status;

}

I also try static keyword but it didnot work.

Abdul Malik
  • 541
  • 4
  • 16

2 Answers2

1

Firebase is a cloud-hosted service. To create a user (in this case), the app needs to make a call to the server, which will spend some time and only then will your user have been created. To prevent blocking the application (which would leave to the infamous Application Not Responding dialog), the Firebase client performs the operation in the background and lets the main thread of your app continue. That's why you'll immediately see the output of System.out.println("user"+status).

You're likely calling this function with something like:

var isCreated = createUserAccount("Abdul Malik", "correcthorsebatterystaple");
if (isCreated) {
  Toast.makeText(getActivity(), "The user was created", Toast.LENGTH_LONG).show();
}
else {
  Toast.makeText(getActivity(), "Failed to create user", Toast.LENGTH_LONG).show();
}

This cannot work, since you cannot block the main thread without creating a horrible user experience. The solution is to invert your logic. Instead of saying "first create a user, then do xyz", rephrase it to "create a user, when that is done do xyz".

You do this by moving the xyz code into the createUserAccount method:

public boolean createUserAccount(String username, String password)throws FirebaseException{

    if(firebase == null)
        firebase = new Firebase(firebase_url);
    firebase.createUser(username+"@firebase.com", password, new Firebase.ValueResultHandler<Map<String, Object>>() {

        @Override
        public void onSuccess(Map<String, Object> result) {
            Toast.makeText(getActivity(), "The user was created", Toast.LENGTH_LONG).show();
            UserAccount.this.status=true;

        }

        @Override
        public void onError(FirebaseError firebaseError) {
            Toast.makeText(getActivity(), "Failed to create user", Toast.LENGTH_LONG).show();
            throw new FirebaseException(firebaseError.getMessage());

        }
    });
    createUserAccount("Abdul Malik", "correcthorsebatterystaple");

See also my answer here: Setting Singleton property value in Firebase Listener

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I knew that method is returning value before it can execute 'onSuccess()' method, but i did not know how to wait for the onSuccess method to execute in the createuseraccount() , so that it returns the new value, thank you for telling the solution – Abdul Malik May 07 '16 at 07:44
0

Just use a regular POJO. Here is an example.

1) Create POJO:

class AnyPojo {

    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

2) Initiate and pass the AnyPojo instance to your method

    AnyPojo mPojo= new AnyPojo();

    AnyPojo returnMyMethod = MyMethod (mPojo);
    returnMyMethod.getStatus(); // <--- This must return "Anything you like"

    public AnyPojo MyMethod(final mPojo) {

        @Override
        public void onSuccess() {
           mPojo.setStatus("Anything you like");
        }

        return mPojo;
    }
Ahmed Adaileh
  • 71
  • 1
  • 4