Android -任务在应该运行的时候没有运行



我使用Backendless来存储我的应用程序用户的数据。Backendless只能选择让你的电子邮件或用户名唯一。我使我的用户名字段唯一,这是什么用户将使用登录。电子邮件可以重复使用,所以我在注册前检查电子邮件是否存在。

这是createUser方法,它开始并调用方法来检查电子邮件的可用性,只要它开始:

private void createUser() {
    showProgressDialog();
    if (isEmailAvailable(inputEmail.getText().toString().trim())) {
        BackendlessUser user = new BackendlessUser();
        user.setProperty(BackendSettings.USERNAME_KEY, inputUsername.getText().toString().trim());
        user.setEmail(inputEmail.getText().toString().trim());
        user.setPassword(inputPassword.getText().toString());
        Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
            @Override
            public void handleResponse(BackendlessUser backendlessUser) {
                hideProgressDialog();
                Toast.makeText(CreateAccountActivity.this, BackendSettings.REGISTER_SUCCESS_MESSAGE, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            @Override
            public void handleFault(BackendlessFault backendlessFault) {
                hideProgressDialog();
                String errorCode = backendlessFault.getCode();
                String errorMessage;
                switch (errorCode) {
                    case "3040":
                        errorMessage = BackendSettings.ERROR_3040;
                        break;
                    case "3033":
                        errorMessage = BackendSettings.ERROR_3033;
                        break;
                    default:
                        errorMessage = "An unknown error occurred. Try again.";
                }
                Toast.makeText(CreateAccountActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }
        });
    } else {
        hideProgressDialog();
        Toast.makeText(CreateAccountActivity.this, BackendSettings.ERROR_EMAIL_TAKEN, Toast.LENGTH_SHORT).show();
    }
}

这是检查邮件可用性的方法:

private boolean isAvailable;
private boolean isEmailAvailable(String email) {
    isAvailable = false;
    String whereClause = "email = '" + email + "'";
    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    dataQuery.setWhereClause(whereClause);
    Backendless.Persistence.of(BackendlessUser.class).find(dataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
        @Override
        public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
            isAvailable = userBackendlessCollection.getData().isEmpty();
        }
        @Override
        public void handleFault(BackendlessFault backendlessFault) {
            Toast.makeText(CreateAccountActivity.this, backendlessFault.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    return isAvailable;
}

好吧,所以我总是得到的消息是,电子邮件已经有人了,即使它没有。因此,我决定在isEmailAvailblehandleResponse方法中放入Toast消息。我打印了这封邮件是否可用。所以我首先从createUser中的else子句中得到消息,说电子邮件已经被占用,然后我从isEmailAvailable方法中得到消息。似乎isEmailAvailable方法正在运行之后。我不知道哪里出了问题。

哇!错误在isEmailAvailable方法中。

查找异步方法。它是这样工作的:你调用它,它开始工作并立即返回,在任何实际结果之前。之后它调用回调方法。所以,当你输入

    return isAvailable;

isAvailable在该行总是false,因为它仍然没有被修改。

你应该做一些异步工作。1. 调用Backendless.Persistence.of (BackendlessUser.class);(…)2. 等待答案。只有在

中才会知道结果
   public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
       isAvailable = userBackendlessCollection.getData().isEmpty();
   }

3。在

之后完成剩下的工作
    isAvailable = userBackendlessCollection.getData().isEmpty();

创建一个doCreateUser方法,将你的"if"的主体放入其中并从handlerresponse调用它,在isAvailable = userBackendlessCollection.getData().isEmpty();

使用以下代码:

    private void createUser() {
        showProgressDialog();
        checkEmailAvailable(inputEmail.getText().toString().trim());
    }
    private void checkEmailAvailable(String email) {
        String whereClause = "email = '" + email + "'";
        BackendlessDataQuery dataQuery = new BackendlessDataQuery();
        dataQuery.setWhereClause(whereClause);
        Backendless.Persistence.of(BackendlessUser.class).find(dataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
            @Override
            public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
                isAvailable = userBackendlessCollection.getData().isEmpty();
                if (isAvailable){
                    doCreateuser();
                } else {
                    hideProgressDialog();
                    Toast.makeText(CreateAccountActivity.this, BackendSettings.ERROR_EMAIL_TAKEN, Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void handleFault(BackendlessFault backendlessFault) {
                Toast.makeText(CreateAccountActivity.this, backendlessFault.getMessage(), Toast.LENGTH_SHORT).show();
                hideProgressDialog();
            }
        });
    }
    private void doCreateuser(){
        BackendlessUser user = new BackendlessUser();
        user.setProperty(BackendSettings.USERNAME_KEY, inputUsername.getText().toString().trim());
        user.setEmail(inputEmail.getText().toString().trim());
        user.setPassword(inputPassword.getText().toString());
        Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
            @Override
            public void handleResponse(BackendlessUser backendlessUser) {
                hideProgressDialog();
                Toast.makeText(CreateAccountActivity.this, BackendSettings.REGISTER_SUCCESS_MESSAGE, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            @Override
            public void handleFault(BackendlessFault backendlessFault) {
                hideProgressDialog();
                String errorCode = backendlessFault.getCode();
                String errorMessage;
                switch (errorCode) {
                    case "3040":
                        errorMessage = BackendSettings.ERROR_3040;
                        break;
                    case "3033":
                        errorMessage = BackendSettings.ERROR_3033;
                        break;
                    default:
                        errorMessage = "An unknown error occurred. Try again.";
                }
                Toast.makeText(CreateAccountActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }
        });
    }

对Backendless的调用是异步的,这就是为什么你的方法总是返回isAvailable初始化的值(这里为false)。例如,在返回正确的值之前,您需要与Semaphore进行同步以阻塞。

private boolean isEmailAvailable(String email) {
boolean isAvailable = false;
String whereClause = "email = '" + email + "'";
final Semaphore s = new Semaphore(0);
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(whereClause);
Backendless.Persistence.of(BackendlessUser.class).find(dataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
    @Override
    public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
        isAvailable = userBackendlessCollection.getData().isEmpty();
        s.release();
    }
    @Override
    public void handleFault(BackendlessFault backendlessFault) {
        Toast.makeText(CreateAccountActivity.this, backendlessFault.getMessage(), Toast.LENGTH_SHORT).show();
        s.release();
    }
});
try{
    s.aquire();
}catch(Exception e){
}
return isAvailable;

}

相关内容

  • 没有找到相关文章

最新更新