如何从匿名类Firebase.ValueResultHandler的onSuccess()方法更新outerscope变量

时间:2016-05-06 13:50:10

标签: android firebase

我打电话给这个班级'来自其他类的方法,我总是得到false,如何在

中更新外部范围数据成员(状态)
  

的onSuccess()

方法,每当我调用此方法时,它总是返回null,任何人都可以告诉我如何才能得到正确的结果

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;

}

我也尝试使用static关键字,但它没有用。

2 个答案:

答案 0 :(得分:1)

Firebase是一种云托管服务。要创建用户(在这种情况下),应用程序需要调用服务器,这将花费一些时间,然后才会创建用户。为了防止阻止应用程序(将留给臭名昭着的Application Not Responding dialog),Firebase客户端在后台执行操作并让应用程序的主线程继续。这就是为什么您会立即看到System.out.println("user"+status)的输出。

您可能会使用以下内容调用此函数:

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

这不起作用,因为您无法在不创建可怕的用户体验的情况下阻止主线程。解决方案是颠倒你的逻辑。而不是说&#34;首先创建一个用户,然后执行xyz&#34;,将其改为&#34;创建用户,完成后再执行xyz&#34;。

您可以通过将xyz代码移动到 createUserAccount方法来执行此操作:

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");

另见我的回答:Setting Singleton property value in Firebase Listener

答案 1 :(得分:0)

只需使用常规的POJO。这是一个例子。

1)创建POJO:

class AnyPojo {

    private String status;

    public String getStatus() {
        return status;
    }

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

2)启动并将AnyPojo实例传递给您的方法

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