Android - Parse的异步验证

时间:2014-08-14 14:46:45

标签: android parse-platform promise

我正在使用Parse来存储我的数据。在用户注册期间,如果用户的电子邮件存在与否,我会创建AsyncTask以在调用活动中设置结果。以下是触发验证的代码

View.OnClickListener btnNextClick = new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        if (etEmail == null) {
            return;
        }

        final String email = etEmail.getText().toString();

        if (email == null || email.length() == 0) {
            etEmail.setError(getResources().getString(
                R.string.error_email_is_null)
            );

            etEmail.requestFocus();
            valid = false;
        } else {
            if (!Common.isValidEmailAddress(email)) {
                etEmail.setError(getResources().getString(R.string.error_email_not_valid));
                etEmail.requestFocus();
                valid = false;
            } else {

                // validate Email from back end
                new CheckEmailAsyncTask(CreateAccountActivity.this, email).execute();

                if (emailValid == false) {
                       etEmail.setError(getResources().getString(R.string.error_email_existed));
                    etEmail.requestFocus();
                    valid = false;
                }
            }
        }

        if (valid) {
            // if valid then going to the next step
            Intent intent = new Intent(CreateAccountActivity.this, UpdateUserActivity.class);
            intent.putExtra(AppConstant.PARAM_EMAIL, email);

            startActivity(intent);
        }
    }

    boolean emailValid;

    public void setEmailValid (boolean emailValid) {
        this.emailValid = emailValid;
    }
};

这是CheckEmailAysncTask的代码

public class CheckEmailAsyncTask extends AsyncTask<String, Void, Void> {
    ProgressDialog progressDialog;
    Context context;
    CreateAccountActivity createAccountActivity;

    String email;

    public CheckEmailAsyncTask(CreateAccountActivity createAccountActivity, String email){
        this.createAccountActivity = createAccountActivity;
        this.context = createAccountActivity;
        this.email = email;

        progressDialog = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        this.progressDialog.show();

        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... params) {
        UserDAO userDAO = new UserDAO(context);
        try {
            int count = userDAO.isUserExists(email);

            if (count > 0) {
                createAccountActivity.setEmailValid(false);
            } else {
                createAccountActivity.setEmailValid(true);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
        super.onPostExecute(result);


     }
    }
}

并在UserDAO

public int isUserExists(String email) throws ParseException {
    ParseQuery query = new ParseQuery("User");

    query.whereEqualTo("email", email);

    return query.count();
}

但是,在我的设置中,AsyncTask下面的代码将首先执行,然后将结果从Parse返回。我怎样才能让其余代码等待将来返回然后继续?我提出的解决方案之一是保持将调用循环到AsyncTask并暂时休眠直到结果返回

2 个答案:

答案 0 :(得分:0)

试试这个:

if (email == null || email.length() == 0) {
    ...
}
else if (email != null & email.length() != 0) {
    ...
}

答案 1 :(得分:0)

我刚刚提出的一个解决方案是向DAO层函数发送回调,因此当触发done动作时,它将触发回调以继续。

public interface NavCallback {
    public void finish();
}


public class MainActivity {
    // inside click listener
     NavCallback navCallbackError = new NavCallback() {
     @Override
     public void finish() {
         setError();
     }    
     .....
}

并且DAO函数将回调作为参数

public void checkExists(String email, NavCallback callback) {
    ..... 
    if (callback != null) callback.finish();
}