title: Cannot access database on the main thread - Android Room - Using ThreadPoolExecutor
body: <p>I'm getting that famous error <code>"Cannot access database on the main thread since it may potentially lock the UI for a long periods of time"</code> But from what I understand, I'm not accessing the database in main thread since I'm executing the call inside a Runnable executed by a ThreadPoolExecutor. What am I doing wrong?</p>
  
  <p>In the following method, I'm using a runnable to obtain data from network and store it in a local database. </p>
  
  <pre><code>private void refresh() {
      executor.execute(() -&gt; {
          if (!dataSource.hasData()) {
              recipeService.getAllRecipes().enqueue(new Callback&lt;List&lt;Recipe&gt;&gt;() {
                  @Override
                  public void onResponse(Call&lt;List&lt;Recipe&gt;&gt; call, Response&lt;List&lt;Recipe&gt;&gt; response) {
                      dataSource.save(response.body());
                  }
  
                  @Override
                  public void onFailure(Call&lt;List&lt;Recipe&gt;&gt; call, Throwable t) {
                      throw new RuntimeException(t);
                  }
              });
          }
  
      });
  }
  </code></pre>
  
  <p>DataSource.save:</p>
  
  <pre><code>@Override
      public void save(List&lt;Recipe&gt; recipes) {
          for (Recipe recipe : recipes) {
              recipeDao.insert(recipe);
              int count = 1;
  
              for (Ingredient ingredient : recipe.getIngredients()) {
                  ingredient.setId(count++);
                  ingredient.setRecipeId(recipe.getId());
                  ingredientDao.insert(ingredient);
              }
  
              for (Step step : recipe.getSteps()) {
                  step.setRecipeId(recipe.getId());
                  stepDao.insert(step);
              }
  
          }
      }
  </code></pre>
  
  <p>The executor is defined as: </p>
  
  <pre><code>@Provides
  Executor executor() {
      return new ThreadPoolExecutor(4, 8, 60,
              TimeUnit.SECONDS, new LinkedBlockingQueue&lt;&gt;());
  }
  </code></pre>
  
  <p>Error I'm getting:</p>
  
  <pre><code>06-18 03:03:38.653 27231-27231/com.github.alexpfx.udacity.nanodegree.android.baking_app E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.github.alexpfx.udacity.nanodegree.android.baking_app, PID: 27231
  java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long periods of time.
  at android.arch.persistence.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:137)
  at android.arch.persistence.room.RoomDatabase.beginTransaction(RoomDatabase.java:184)
  at com.github.alexpfx.udacity.nanodegree.android.baking_app.data.local.RecipeDao_Impl.insert(RecipeDao_Impl.java:89)
  at com.github.alexpfx.udacity.nanodegree.android.baking_app.recipe.RecipeLocalDataSource.save(RecipeLocalDataSource.java:34)
  at com.github.alexpfx.udacity.nanodegree.android.baking_app.recipe.RecipesRepositoryImpl$1.onResponse(RecipesRepositoryImpl.java:49)
  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
  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:6776)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
  </code></pre>