简体   繁体   English

Android在完成语句之前不等待数据库响应

[英]Android not waiting for DB response before finishing statement

I have an interesting problem that I've never run into in programming before. 我有一个有趣的问题,以前从未在编程中遇到过。 I have an onClickListener that does a lot of username and password checks (makes sure the username is proper length, not taken, etc). 我有一个onClickListener,它执行很多用户名和密码检查(确保用户名正确长度,未使用等)。 I'm using MobDB, and I was using a conditional statement that would return a row if the username already existed. 我正在使用MobDB,并且正在使用条件语句,如果用户名已经存在,该条件语句将返回一行。 The problem is that the Listener skips the DB and goes to the final check that, if everything works, posts a new username and password to my DB. 问题在于,侦听器会跳过数据库,并进行最终检查,如果一切正常,则将新的用户名和密码发布到我的数据库中。 How can I make it wait for a response from the DB before skipping to the last check? 在跳转到最后检查之前,如何使它等待数据库的响应?

Here is the relevant code: 以下是相关代码:

usernamecheck3 = true;
                    MobDB.getInstance().execute(APP_KEY, null, rd, null, false, new MobDBResponseListener() {

                        @Override public void mobDBSuccessResponse() {
                            usernamecheck3 = false;
                            Log.e("mobdbSuccess:", "success");
                        }           

                        @Override public void mobDBResponse(Vector<HashMap<String, Object[]>> row) {
                        }        

                        @Override public void mobDBResponse(String jsonObj) {
                            /*Log.e("mobdbSuccess:", "jsonObj");
                            Log.e("mobdbSuccess:", jsonObj);
                            JSONObject mainObject;
                            try {
                                mainObject = new JSONObject(jsonObj);
                                // need to parse the json object.
                            } catch (JSONException e1) {

                                e1.printStackTrace();
                            } */
                        } 

                        @Override public void mobDBFileResponse(String fileName, byte[] fileData) {
                        //get file name with extension and file byte array
                        }           

                        @Override public void mobDBErrorResponse(Integer errValue, String errMsg) {
                            usernamecheck3 = false;
                            Log.e("doesnt", "work");
                        }
                    }); 
                    if(usernamecheck3 == false){
                        Toast.makeText(getApplicationContext(), "Username is taken, please choose another", Toast.LENGTH_SHORT).show();
                    }

Basically the check always returns true, and then logcat will say mobdbSuccess: success, which should have set the Bool to false. 基本上检查总是返回true,然后logcat会说mobdbSuccess:成功,应该将Bool设置为false。

Thanks. 谢谢。

MobDBResponseListener is executing on a different thread. MobDBResponseListener正在另一个线程上执行。 What happens here is that the processing is split, while a thread is doing the query, the main thread on which you added the listener, skips right ahead to the validation. 这里发生的是,处理被拆分了,而线程正在执行查询,即在其上添加了侦听器的主线程就直接跳到了验证。 Your best bet is to place the validation inside the MobDBResponseListener, on the mobDBResponse method. 最好的选择是将验证放在mobDBResponse方法的MobDBResponseListener中。 Try to debug your code and calls, the Listener may be using an async task. 尝试调试代码和调用,侦听器可能正在使用异步任务。 If so, you may do anything you please from the response method, as it will be executing in the main thread again. 如果是这样,您可以通过response方法执行任何操作,因为它将在主线程中再次执行。 Otherwise, you should look at solutions that handle threaded execution like Handlers 否则,您应该查看处理线程执行的解决方案,例如Handlers。

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

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