Reputation: 528
In my app, I want users to be able to register themselves. If another user before them has already used that particular emailadress or username the app should say so and not let them upload data to the server.
Right now my problem is, that the main thread does not wait for the two background tasks that check if username or email already exist, but keeps going so every user object is sent to the server, even though they already exist.
Here is my code to check username and email:
public void checkEmailadress(String s){
ParseQuery<ParseObject> query = ParseQuery.getQuery("userLogin");
query.whereEqualTo("emailadress",s);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> arg0, ParseException e) {
// TODO Auto-generated method stub
Iterator itr = arg0.iterator();
if(itr.hasNext()){
emailadressInUse = true;
}else{
emailadressInUse = false;
}
System.out.println(emailadressInUse);
finishSubmittingUser();
}
});
}
same goes for checking the username. This works fine. The problem is, boolean emailadressInUse does not change fast enough for the main thread to change action.
here is the method that initiates those two methods
public void submitNewUser(View view){
EditText edittext1 = (EditText) findViewById(R.id.chose_username);
EditText edittext2 = (EditText) findViewById(R.id.chose_realname);
EditText edittext3 = (EditText) findViewById(R.id.chose_emailadress);
EditText edittext4 = (EditText) findViewById(R.id.chose_password);
username = edittext1.getText().toString();
realname = edittext2.getText().toString();
emailadress = edittext3.getText().toString();
password = edittext4.getText().toString();
checkUsername(username);
checkEmailadress(emailadress);
}
this is called from inside checkEmail. The two booleans should have changed state by now. And according to System.out.println in checkEmail they did. So why is my method finishSubmittingUser not picking up on that change?
public void finishSubmittingUser(){
if((usernameInUse==false) && (emailadressInUse==false)){
saveDataChange("realname", realname);
ParseObject userLogin = new ParseObject("userLogin");
userLogin.put("username", username);
userLogin.put("emailadress", emailadress);
userLogin.put("password", password);
userLogin.saveInBackground();
Intent intent = new Intent(this, LogInUserActivity.class);
intent.putExtra(USERNAME, username);
startActivity(intent);
}
else{
if(usernameInUse==true){
Toast.makeText(getApplicationContext(),getString(R.string.username_in_use),Toast.LENGTH_SHORT).show();
}
if(emailadressInUse==true){
Toast.makeText(getApplicationContext(),getString(R.string.emailadress_in_use),Toast.LENGTH_SHORT).show();
}
}
}
There must be an easy way to do this that i´m not seeing right now.
Thanks for your help.
Upvotes: 0
Views: 238
Reputation: 1006724
Make the main thread wait for backgroundtasks to finish?
As Emmanuel said, do not block the main application thread.
Right now my problem is, that the main thread does not wait for the two background tasks that check if username or email already exist, but keeps going so every user object is sent to the server, even though they already exist.
If "every user object is sent to the server" is something the user does, after registration, simply disable the UI for that (e.g., disable the action bar item) until your registration is confirmed.
If "every user object is sent to the server" is something that happens automatically after a successful registration, then you should not be executing that code until after a successful registration.
Upvotes: 5