title: Android Room: offline caching doesn't work
body: <p>I have followed <a href="https://developer.android.com/codelabs/kotlin-android-training-repository" rel="nofollow noreferrer">this</a> course trying to implement online caching in my app. Although everything looks similar, when I turn on airplane mode, data doesn't show. Here is my code:</p>
  <p><strong>Repository</strong></p>
  <pre><code>class WeatherRepository(private val database: WeatherDatabase) {
  
      var weather: LiveData&lt;CurrentWeather&gt; = Transformations.map(database.weatherDao.getWeather()){
          it.asDomainModel()
      }
  
      suspend fun refreshWeather(city: String){
          withContext(Dispatchers.IO){
              val weather = WeatherNetwork.service.getCurrentWeather(&quot;London&quot;, &quot;metric&quot;)
              database.weatherDao.insert(weather.asDatabaseModel())
              Log.i(&quot;INSERTING: &quot;, weather.asDatabaseModel().toString())
          }
      }
  
      //I've created this function only for debugging
      fun getDataFromDB(){
          weather = Transformations.map(database.weatherDao.getWeather()){
              it.asDomainModel()
          }
          val data = database.weatherDao.getWeather()
          Log.i(&quot;DB VALUES: &quot;, data.value.toString())
          Log.i(&quot;VAR WEATHER IN REPO: &quot;, weather.value.toString())
      }
  }
  </code></pre>
  <p><strong>Room Database</strong></p>
  <pre><code>@Dao
  interface WeatherDao{
  
      @Query(&quot;select * from current_weather&quot;)
      fun getWeather(): LiveData&lt;DatabaseWeather&gt;
  
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      fun insert(weather: DatabaseWeather)
  }
  
  @Database(entities = [DatabaseWeather::class], version = 1)
  abstract class WeatherDatabase: RoomDatabase(){
      abstract val weatherDao: WeatherDao
  }
  
  private lateinit var INSTANCE: WeatherDatabase
  
  fun getDatabase(context: Context): WeatherDatabase{
      synchronized(WeatherDatabase::class.java){
          if (!::INSTANCE.isInitialized){
              INSTANCE = Room.databaseBuilder(context.applicationContext,
              WeatherDatabase::class.java, &quot;weather_db&quot;).build()
          }
      }
      return INSTANCE
  }
  </code></pre>
  <p><strong>Entity</strong></p>
  <pre><code>const val WEATHER_ID = 0
  
  @Entity(tableName = &quot;current_weather&quot;)
  data class DatabaseWeather constructor(
      val name: String,
      val lon: Double,
      val lat: Double,
      val description: String,
      val icon: String,
      val temp: Double,
      val tempFeelsLike: Double,
      val humidity: Int,
      val pressure: Int
  ){
      @PrimaryKey(autoGenerate = false)
      var id: Int = WEATHER_ID
  }
  
  fun DatabaseWeather.asDomainModel(): CurrentWeather{
      return CurrentWeather(
          name = this.name,
          lon = this.lon,
          lat = this.lat,
          description = this.description,
          icon = this.icon,
          temp = this.temp,
          tempFeelsLike = this.tempFeelsLike,
          humidity = this.humidity,
          pressure = this.pressure
      )
  }
  </code></pre>
  <p><strong>Network</strong></p>
  <pre><code>const val BASE_URL = &quot;https://api.openweathermap.org/data/2.5/&quot;
  const val API_KEY = &quot;xxx&quot;
  
  private val moshi = Moshi.Builder()
      .add(KotlinJsonAdapterFactory())
      .build()
  
  interface WeatherApiService {
      @GET(&quot;weather&quot;)
      suspend fun getCurrentWeather(
          @Query(&quot;q&quot;) cityName: String,
          @Query(&quot;units&quot;) units: String
      ): WeatherResponse
  }
  
  object WeatherNetwork{
      private val requestInterceptor = Interceptor { chain -&gt;
          val url = chain.request()
              .url()
              .newBuilder()
              .addQueryParameter(&quot;appid&quot;, API_KEY)
              .build()
  
          val request = chain.request()
              .newBuilder()
              .url(url)
              .build()
  
          return@Interceptor chain.proceed(request)
      }
  
      private val okHttpClient = OkHttpClient.Builder()
          .addInterceptor(requestInterceptor)
          .build()
  
      private val retrofit = Retrofit.Builder()
          .client(okHttpClient)
          .baseUrl(BASE_URL)
          .addConverterFactory(MoshiConverterFactory.create(moshi))
          .build()
  
      val service = retrofit.create(WeatherApiService::class.java)
  }
  </code></pre>
  <p><strong>ViewModel</strong></p>
  <pre><code>enum class WeatherApiStatus { LOADING, ERROR, DONE }
  
  class CurrentWeatherViewModel(application: Application) : AndroidViewModel(application) {
  
      val place = MutableLiveData&lt;String&gt;()
  
      private val _status = MutableLiveData&lt;WeatherApiStatus&gt;()
  
      val status: LiveData&lt;WeatherApiStatus&gt;
          get() = _status
  
      private val weatherRepository = WeatherRepository(getDatabase(application))
  
      val weather = weatherRepository.weather
  
      init {
          refreshDataFromRepository()
      }
  
      fun refreshDataFromRepository(){
          viewModelScope.launch {
              try{
                  weatherRepository.refreshWeather(place.value.toString())
                  _status.value = WeatherApiStatus.DONE
              } catch (e: Exception){
                  Log.i(&quot;FAILURE: &quot;, e.printStackTrace().toString())
                  _status.value = WeatherApiStatus.ERROR
              }
          }
      }
  
      fun checkDB(){
          weatherRepository.getDataFromDB()
      }
  }
  </code></pre>
  <p>It is working, when I don't call method refreshWeather in WeatherRepository (the data is downloaded from db).
  I don't know why, but logs &quot;DB VALUES&quot; and &quot;VAR WEATHER IN REPO&quot; always show null (&quot;INSERTING&quot; shows data properly)</p>