title: Saving data to Room from androidx.work.Worker fails
body: <p>I´ve tried saving data from a Worker to Room database.</p>
  
  <p><strong>Versions:</strong></p>
  
  <ul>
  <li>Work Runtime Ktx - android.arch.work:work-runtime-ktx:1.0.0-alpha02</li>
  <li>Room - android.arch.persistence.room:runtime:1.1.0 and
  android.arch.persistence.room:compiler:1.1.0</li>
  </ul>
  
  <p><strong>MainActivity.kt</strong></p>
  
  <pre><code>private fun runDataDownloadWork() {
          WorkManager.getInstance()
                  .beginWith(dwElementTypesWork)
                  .then(dwElementsWork)
                  .enqueue()
          WorkManager.getInstance().getStatusById(dwElementsWork!!.id)
                  .observe(this, Observer { workStatus -&gt;
                      if (workStatus != null &amp;&amp; workStatus.state.isFinished) {
                          Log.d("WorkManager", "dwElementsWork finished")
                      }
                  })
      }
  
      private fun createWorkRequests() {
          dwElementsWork = OneTimeWorkRequestBuilder&lt;DWElementsWork&gt;()
                  .addTag(DWElementsWork.TAG)
                  .setConstraints(wifiConstraints)
                  .build()
          dwElementTypesWork = OneTimeWorkRequestBuilder&lt;DWElementTypesWork&gt;()
                  .addTag(DWElementTypesWork.TAG)
                  .setConstraints(wifiConstraints)
                  .build()
      }
  </code></pre>
  
  <p><strong>DWElementsWork.kt</strong></p>
  
  <pre><code>class DWElementsWork : Worker() {
  
      companion object {
          const val TAG = "dw_elements_work"
      }
  
      override fun doWork(): WorkerResult {
  
          val apiService: APIService = ServiceGenerator
              .createService(applicationContext, MyAPIService::class.java)
          var elementDao: ElementDao? = AppDatabase 
              .getInstance(context = applicationContext)
              .elementDao()
  
          val response = apiService.getElements().execute()
          if (response.isSuccessful) {
              val elements = response.body()
              try {
                  elementDao?.insertElements(elements!!)
              } catch (e: Exception) {
                  e.printStackTrace()
              } finally {
                  return WorkerResult.SUCCESS
              }
          }
          Log.e("BACKGROUND_WORKER", response.errorBody()?.string())
          return WorkerResult.FAILURE
      }
  }
  </code></pre>
  
  <p><strong>AppDatabase.kt</strong></p>
  
  <pre><code>@Database(entities = [
      (Element::class)
  ], version = 9, exportSchema = false)
  abstract class AppDatabase: RoomDatabase() {
  
      abstract fun elementDao(): ElementDao
  
      companion object {
  
          private var sInstance: AppDatabase? = null
  
          /**
           * Gets the singleton instance of AppDatabase.
           *
           * @param context The context.
           * @return The singleton instance of AppDatabase.
           */
          @Synchronized
          fun getInstance(context: Context): AppDatabase {
              if (sInstance == null) {
                  sInstance = Room
                          .databaseBuilder(
                              context.applicationContext,
                              AppDatabase::class.java,
                              "my_database.db"
                          )
                          .fallbackToDestructiveMigration()
                          .build()
              }
              return sInstance!!
          }
      }
  }
  </code></pre>
  
  <p>The main problem resides when trying to call <code>elementDao?.insertElements(elements!!)</code> it gives the follwing error:</p>
  
  <p><strong>StackTrace</strong></p>
  
  <pre><code>06-20 15:57:09.660 25193-25226/com.myapp.app E/ROOM: Cannot run invalidation tracker. Is the db closed?
      java.lang.IllegalStateException: The database '/data/user/0/com.myapp.app/databases/my_database.db' is not open.
          at android.database.sqlite.SQLiteDatabase.throwIfNotOpenLocked(SQLiteDatabase.java:2287)
          at android.database.sqlite.SQLiteDatabase.createSession(SQLiteDatabase.java:409)
          at android.database.sqlite.-$$Lambda$RBWjWVyGrOTsQrLCYzJ_G8Uk25Q.get(Unknown Source:2)
          at java.lang.ThreadLocal$SuppliedThreadLocal.initialValue(ThreadLocal.java:284)
          at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:180)
          at java.lang.ThreadLocal.get(ThreadLocal.java:170)
          at android.database.sqlite.SQLiteDatabase.getThreadSession(SQLiteDatabase.java:403)
          at android.database.sqlite.SQLiteProgram.getSession(SQLiteProgram.java:101)
          at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
          at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeUpdateDelete(FrameworkSQLiteStatement.java:45)
          at android.arch.persistence.room.InvalidationTracker$1.run(InvalidationTracker.java:321)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
          at java.lang.Thread.run(Thread.java:764)
  </code></pre>
  
  <p>I´ve stopped the debugger right before inserting the elements and <code>AppDatabase.getInstance(applicationContext).isOpen</code> returns <strong>false</strong>.</p>
  
  <p>Am I doing something wrong?</p>
  
  <p>Is it possible to save data from Workers to Room?</p>
  
  <p>Maybe <strong>applicationContext</strong> (getApplicationContext()) is not giving the appropriate context and AppDatabase it´s badly instantiated?</p>
  
  <p>Thanks in advance!</p>