title: Singleton Room database in SyncAdapter to trigger LiveData
body: <p>I'm struggling to use <code>Room</code> as singleton in my <code>SyncAdapter</code>. I use Kotlin.</p>
  
  <p>My room class</p>
  
  <pre><code>@Database(entities = [(Product::class)], version = 1, exportSchema = false)
  abstract class AppDatabase : RoomDatabase() {
      abstract fun productDao(): ProductDao
  
      companion object {
          @Volatile private var INSTANCE: AppDatabase? = null
  
          fun getInstance(context: Context) : AppDatabase =
              INSTANCE ?: synchronized(this) {
                  INSTANCE ?: buildDatabase(context.applicationContext)
                          .also {INSTANCE = it}
              }
  
          private fun buildDatabase(context: Context) =
                  Room
                          .databaseBuilder(context.applicationContext,
                                  AppDatabase::class.java, "database.db")
                          .allowMainThreadQueries()
                          .build()
      }
  
  }
  </code></pre>
  
  <p>I retrieve database instance like this </p>
  
  <pre><code>val db = AppDatabase.getInstance(applicationContext)
  </code></pre>
  
  <p>And my issue is that I always get two different instances of <code>AppDatabase</code> in my activities and <code>SyncAdapter</code>. Though among activities <code>AppDatabase</code> object is indeed singleton <code>.AppDatabase_Impl@3c9ff34d</code>, and for each onPerformSync() the <code>AppDatabase</code> is singleton too <code>.AppDatabase_Impl@7d7718d</code>. 
  But as you can see they are two different objects.
  Can any one explain what I miss here?</p>
  
  <p>On the other hand maybe I'm conceptually wrong in what I'm trying to achieve. Then any advice would be appreciated.</p>
  
  <p>The point is to use <code>LiveData</code> to update UI components when new data is inserted from remote server via <code>SynAdapter</code>. In this case I have to use the same <code>productDao</code> object in <code>ViewModel/Activity</code> and in <code>SyncAdapter</code> in order to trigger LiveData on inserting new products, otherwise it won't be triggered.
  So, to obtain the same <code>productDao</code> I must get the same(i.e. singleton) <code>AppDatabase</code>.</p>
  
  <p>I know that this can be implemented using a <code>ContentProvider</code>, that is triggered automatically on inserting new data. But I really want to try new android architecture components. 
  Or maybe using <code>ContentProvider</code> is the only correct way to implement this use-case?</p>