Search code examples
androidsqlitehttpasynchronousblock

Amount of Sqlite operation block the UI in Android Asynchronous Http Client onSuccess()


@Override
public void onStart() {
    setCharset("GB2312");
    progess_bar_PopupWindow();
}

@Override
public void onSuccess(String response) {
    String[] split_array = JwcRegex
            .parse_department_list(response);
    Sql.dep_update(split_array);//here contain amount of sql insert operation
    Toast.makeText(getApplicationContext(), "hello rex",
            Toast.LENGTH_LONG).show();
    if (pw_progress_window.isShowing()) {
        pw_progress_window.dismiss();
    }
}

When the http request start, progess_bar_PopupWindow() show the popwindow which has a progressbar, but the progress bar is stilling, after the request it closed in my onSuccess(). but removed the Sql.dep_update(split_array), the progress bar works well. how to deal?


Solution

  • I solved this problem. As follows:

    1. add synchronized with query and exec method in Sql class.
      Some error caused by concurrent read/write sqlite. The synchronized keyword could synchronized the operation

    2. put popwindow(with my progressbar in it) showing method showAtLocation() in onWindowFocusChanged() method.

    3. put the method which has amount of operation in a new thread. code as follow:

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasLoad) {
            get_departments();
            hasLoad = true;
        }
    }
    
    public void exec(String sql) {
        synchronized (this) {
            db.execSQL(sql);
        }
    }
    
    @Override
    public void onSuccess(String response) {
        Yangtzeu.TempString = JwcRegex
            .parse_department_list(response);
    
        if (pw_progress_window.isShowing()) {
            pw_progress_window.dismiss();
        }
    
        new Thread(new Runnable() {
            public void run() {
                JwcDB.dep_update(Yangtzeu.TempString);
                ComDB.kv_set("dep_list_exp", "false");
            }
        }).start();
    }