title: Observe Data from Room using View model in Kotlin
body: <p>I am creating a social media app and I am following <code>MVVM</code> Pattern
  I am stuck at <code>observing</code> data from DB . As far I know is the <code>Dao</code> methods have to be executed on the background <code>thread</code>. But I am not able to implement <code>viewmodel</code> from <code>Repository</code> Class.I will paste the code ,Can anyone give me some help?</p>
  
  <p><strong>Dao</strong></p>
  
  <pre><code>@Dao
  interface FeedDao{
  
  //Post feed item
  @Insert(onConflict = OnConflictStrategy.IGNORE)
  fun insert(feed:Feed):Long
  
  //Update feed item
  @Update(onConflict = OnConflictStrategy.IGNORE)
  fun update(feed: Feed):Int
  
  //Get feed from user friend's
  @Query("SELECT * from feed")
  fun getAllFeed():LiveData&lt;List&lt;Feed&gt;&gt;
  }
  </code></pre>
  
  <p><strong>Repository</strong></p>
  
  <pre><code>class FeedRepository private constructor(private val feedDao: FeedDao) {
  
  companion object {
      @Volatile private var instance:FeedRepository? = null
      private val apiClient = ApiClient.getApiClient().create(UserClient::class.java)
      fun getInstance(feedDao: FeedDao)= instance?: synchronized(this){
        instance ?: FeedRepository(feedDao).also { instance=it }
       }
  }
  
  
  private fun getFeedResponse() {
  
      //TODO:Check to change parameter values after adding Paging Functionality
      val call: Call&lt;List&lt;Feed&gt;&gt; = apiClient.getFeed(20, 0)
      call.enqueue(object :Callback&lt;List&lt;Feed&gt;&gt;{
          override fun onFailure(call: Call&lt;List&lt;Feed&gt;&gt;?, t: Throwable?) {
              Log.e("FeedRepository","Feed Callback Failure")
              Timber.d(t?.message)
          }
  
          override fun onResponse(call: Call&lt;List&lt;Feed&gt;&gt;?, response: Response&lt;List&lt;Feed&gt;&gt;?) {
              if(response!!.isSuccessful){
                  Timber.i("Successful Response -&gt; Adding to DB")
                  addResponseTODB(response.body()!!)
              }else{
                  when(response.code()){
                      400 -&gt; Timber.d("Not Found 400")
                      500 -&gt; Timber.d("Not logged in or Server broken")
                  }
  
              }
  
          }
      })
  }
  
  private fun addResponseTODB(items:List&lt;Feed&gt;){
      Timber.d("Response --&gt; DB Started")
      object :  AsyncTask&lt;List&lt;Feed&gt;,Void,Boolean&gt;(){
          override fun doInBackground(vararg params: List&lt;Feed&gt;?): Boolean {
              var needsUpdate:Boolean = false
              for(item in params[0]!!.iterator()){
                  var inserted = feedDao.insert(item)
                  if(inserted.equals(-1)){
                      var updated = feedDao.update(item)
                      if(updated &gt; 0){
                          needsUpdate = true
                      }
                  }else{
                      needsUpdate = true
                  }
              }
              return needsUpdate
          }
  
          override fun onPostExecute(result: Boolean?) {
              if (result!!){
                  loadFromDB()
              }
          }
  
      }.execute(items)
  }
  
  
  private fun loadFromDB(){
      Timber.d("loadFromDB")
  
      object :AsyncTask&lt;Void,Void,LiveData&lt;List&lt;Feed&gt;&gt;&gt;(){
          override fun doInBackground(vararg params: Void?): LiveData&lt;List&lt;Feed&gt;&gt;? {
              return feedDao.getAllFeed()
          }
  
          override fun onPostExecute(result: LiveData&lt;List&lt;Feed&gt;&gt;?) {
  
          }
      }.execute()
  }
  
  
  public fun fetchFeed(){
      Timber.d("fetchFeed")
      loadFromDB()
      getFeedResponse()
  }
  
  }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>class FeedViewModel private constructor(private val 
  feedDao:FeedDao):ViewModel(){
  // TODO: Implement the ViewModel
  
  private val feedRepository= FeedRepository.getInstance(feedDao)
  public val feed = MediatorLiveData&lt;List&lt;Feed&gt;&gt;()
  val value = MutableLiveData&lt;Int&gt;()
  init {
          feed.addSource(feed,feed::setValue)
  }
  
  companion object {
      private const val NO_FEED = -1
  }
  
  fun getFeedLD() = feed
  
  }
  </code></pre>