简体   繁体   English

使主线程等待backgroundtasks完成?

[英]Make the main thread wait for backgroundtasks to finish?

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. 问题是,布尔emailadressInUse的更改速度不足以使主线程更改操作。

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. 这是从checkEmail内部调用的。 The two booleans should have changed state by now. 这两个布尔值现在应该已经改变了状态。 And according to System.out.println in checkEmail they did. 并根据checkEmail中的System.out.println进行了操作。 So why is my method finishSubmittingUser not picking up on that change? 那么,为什么我的方法finishSubmittingUser无法接受该更改?

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. 谢谢你的帮助。

Make the main thread wait for backgroundtasks to finish? 使主线程等待backgroundtasks完成?

As Emmanuel said, do not block the main application thread. 正如Emmanuel所说的,不要阻塞主应用程序线程。

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 (eg, disable the action bar item) until your registration is confirmed. 如果“每个用户对象都发送到服务器”是用户执行的操作,则在注册后,只需为此禁用UI(例如,禁用操作栏项),直到确认您的注册。

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 . 如果“每个用户对象被发送到服务器”的东西,注册成功后自动发生,那么你不应该执行该代码,直到注册成功后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM