title: Android Database operation in Asynctask blocks UI Thread
body: <p>I'm performing a Database operation in the <code>doInBackground</code> method inside an <code>AsyncTask</code> on <code>Android</code>.</p>
  
  <p>For some reason, the <code>UI</code> get blocked during the 5-6 seconds that the operation takes.</p>
  
  <p>It does not make any sense for me, the operacion inside the <code>doInBackground</code> should not be executed in the <code>UI Thread</code>, right?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>private class CountItems extends AsyncTask&lt;String, Void, Integer&gt; {
      private ProgressDialog dialog;
  
      @Override
      protected void onPreExecute() {
          dialog = new ProgressDialog(context);
          dialog.setCancelable(false);
          dialog.setMessage(getString(R.string.asynTask_loading));
          dialog.show();
      }
  
      @Override
      protected Integer doInBackground(String... params) {
          // This operation takes 5-6 seconds.
          return app.databaseSession().getMyObjectDao().count(selectedProject, filter, null, false);
      }
  
      @Override
      protected void onPostExecute(Integer result) {
          counterTextView.setText(String.valueOf(result));
  
          if (dialog.isShowing()) {
              dialog.dismiss();
          }
      }
  }
  </code></pre>
  
  <p>I've made a test. If I put a <code>Thread.sleep()</code> inside the <code>doInBackground</code> method, it is executed in a different <code>Thread</code> without blocking the <code>UI</code>.</p>
  
  <p>Like this:</p>
  
  <pre><code> @Override
      protected Integer doInBackground(String... params) {
          try {
              Thread.sleep(3000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          return 0;
      }
  </code></pre>
  
  <p>Any ideas?
  Thanks in advance.</p>