title: how save data to database after fetching data from server using room in kotlin coroutines?
body: <p>I am developing news app I want to save data to database after fetching data from server using room but I am just confused I have followed tutorial but their logics is different how can I save data properly so that after getting data from server and I can save data to database?</p>
  
  <p>below my SportNewsDatabase.kt</p>
  
  <pre><code>@Database(entities = [Article::class], version = 1, exportSchema = false)
  abstract class SportNewsDatabase : RoomDatabase() {
  
      abstract fun sportNewsDao(): SportNewsDao
  
      companion object {
          private var instance: SportNewsDatabase? = null
          fun getInstance( context: Context): SportNewsDatabase? {
              if (instance == null) {
                  synchronized(SportNewsDatabase::class.java) {
                      instance = Room.databaseBuilder(context.applicationContext, SportNewsDatabase::class.java, "article_database")
                          .fallbackToDestructiveMigration()
                          .build()
                  }
              }
              return instance
          }
      }
  
  
  }
  </code></pre>
  
  <p>below SportNewsDao.kt</p>
  
  <p>@Dao
  interface SportNewsDao {</p>
  
  <pre><code>@Query("SELECT * FROM  article")
  fun getAllData(): LiveData&lt;List&lt;Article&gt;&gt;
  
  @Insert
   suspend fun addAll(article: List&lt;Article&gt;)
  
  
  @Update
   suspend fun updateArticle(article: Article)
  
  @Delete
   suspend fun deleteArticle(article: Article)
  </code></pre>
  
  <p>}</p>
  
  <pre><code>below my repository class
  interface NewsRepository {
      // Suspend is used to await the result from Deferred
      suspend fun getNewsList(): UseCaseResult&lt;List&lt;Article&gt;&gt;
       var sportNewsDao: SportNewsDao
  }
  
  
  
  fun insertArticle(article: List&lt;Article&gt;){
      val insertArticles 
  }
  
  
  @Suppress("UNCHECKED_CAST")
  class NewsRepositoryImpl(private val sportsNewsApi: SportNewsInterface) : NewsRepository {
      override suspend fun getNewsList(): UseCaseResult&lt;List&lt;Article&gt;&gt; {
  
          return try {
              val result = sportsNewsApi.getNewsAsync().body()!!.articles
  
              UseCaseResult.Success(result)
          } catch (ex: Exception) {
              UseCaseResult.Error(ex)
          }
      }
  }
  </code></pre>
  
  <p>below MainViewModel.kt where I am using Kotlin coroutines</p>
  
  <pre><code>@Suppress("UNCHECKED_CAST")
  class MainViewModel(val newsRepository: NewsRepository) : ViewModel(), CoroutineScope {
      // Coroutine's background job
      val job = Job()
      // Define default thread for Coroutine as Main and add job
      override val coroutineContext: CoroutineContext = Dispatchers.Main + job
  
      val showLoading = MutableLiveData&lt;Boolean&gt;()
      val sportList = MutableLiveData&lt;List&lt;Article&gt;&gt;()
      val showError = SingleLiveEvent&lt;String&gt;()
  
      fun loadNews() {
          // Show progressBar during the operation on the MAIN (default) thread
          showLoading.value = true
          // launch the Coroutine
          launch {
              // Switching from MAIN to IO thread for API operation
              // Update our data list with the new one from API
              val result = withContext(Dispatchers.IO) {
                  newsRepository?.getNewsList()
              }
              // Hide progressBar once the operation is done on the MAIN (default) thread
              showLoading.value = false
              when (result) {
  
                  is UseCaseResult.Success&lt;*&gt; -&gt; {
                      sportList.value = result.data as List&lt;Article&gt;
                  }
                  is Error -&gt; showError.value = result.message
              }
          }
      }
  
      override fun onCleared() {
          super.onCleared()
          // Clear our job when the linked activity is destroyed to avoid memory leaks
          job.cancel()
      }
  }
  </code></pre>