title: Room Database - AsyncTask Not Executing
body: <p>I have implemented a Room Database and have a number of query transactions that run in a separate thread from the UI.  This particular query is getting an exception for <strong>Cannot access database on the main thread since it may potentially lock the UI for a long period of time</strong>. I cannot see why this particular transaction is attempting to run on the UI thread. <strong>Attached is the illegal state exception.</strong> </p>
  
  <pre><code>2020-05-06 06:40:33.365 23022-23022/com.grgapps.checkingin E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.grgapps.checkingin, PID: 23022
      java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
          at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:267)
          at androidx.room.RoomDatabase.query(RoomDatabase.java:323)
          at androidx.room.util.DBUtil.query(DBUtil.java:83)
          at com.grgapps.checkingin.ContactTableDao_Impl.findContactByContactTablePrimaryKey(ContactTableDao_Impl.java:350)
          at com.grgapps.checkingin.CheckInContactsFilterAndDisplayAdapter.deleteItemFromContactListDisplay(CheckInContactsFilterAndDisplayAdapter.java:116)
          at com.grgapps.checkingin.CheckInContactsFilterAndDisplayAdapter$1.onClick(CheckInContactsFilterAndDisplayAdapter.java:178)
          at android.view.View.performClick(View.java:5610)
          at android.view.View$PerformClick.run(View.java:22265)
          at android.os.Handler.handleCallback(Handler.java:751)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:6077)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
  </code></pre>
  
  <p><strong>This is the Contact Table.</strong></p>
  
  <pre><code>package com.grgapps.checkingin;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.provider.ContactsContract;
  import android.widget.QuickContactBadge;
  
  import androidx.annotation.NonNull;
  import androidx.room.Entity;
  import androidx.room.Index;
  import androidx.room.PrimaryKey;
  
  import java.sql.Blob;
  import java.util.BitSet;
  
  @Entity(tableName = "contact_table",
      indices = {@Index("contactTablePrimaryKey"),
      @Index("contactContractsKey")})
  
  
      public class ContactTable {
  
      @PrimaryKey(autoGenerate = true)
      @NonNull
      private int contactTablePrimaryKey;
  
      @NonNull
      private String contactContractsKey;
  
      @NonNull
  
      private String contactDisplayName;
  
      @NonNull
      private String contactLastName;
  
      @NonNull
      private String contactFirstName;
  
      private String contactMiddleName;
  </code></pre>
  
  <p><strong>This is the code initiating the query</strong></p>
  
  <pre><code>ContactTable contactFindContactByContactTablePrimaryKeyQuery = contactTableDao
                  .findContactByContactTablePrimaryKey(mContactTablePrimaryKey);
  
  </code></pre>
  
  <p><strong>This is the Room Database DataAccessObject</strong></p>
  
  <pre><code>   @Query("SELECT * FROM contact_table WHERE contactTablePrimaryKey = :contactTablePrimaryKey")
      public abstract ContactTable findContactByContactTablePrimaryKey(int contactTablePrimaryKey);
  </code></pre>
  
  <p><strong>This is the Room Database Table Repository with the Async Task code</strong></p>
  
  <pre><code>private static class FindContactByContactTablePrimaryKeyQueryAsyncTask extends
              AsyncTask&lt;Integer, Void, ContactTable&gt; {
  
          private ContactTableDao asyncTaskDao;
          private ContactTableRepository delegate = null;
  
          FindContactByContactTablePrimaryKeyQueryAsyncTask(ContactTableDao dao) {
              asyncTaskDao = dao;
          }
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
          }
  
          @Override
          protected ContactTable doInBackground(final Integer... params) {
              Log.i(TAG, "ContactTableRepository doInBackground: ");
              return asyncTaskDao.findContactByContactTablePrimaryKey(params[0]);
          }
          @Override
          protected void onPostExecute(ContactTable result) {
              delegate.findContactByContactTablePrimaryKeyAsyncFinished(result);
          }
      }
  private void findContactByContactTablePrimaryKeyAsyncFinished(ContactTable results) {
          Log.i(TAG, "ContactTableRepository asyncFinished: ");
          foundContactByContactTablePrimaryKey = results;
      }
  '''
  
  
  </code></pre>