title: How to use MutableStateFlow to save cache in android?
body: <p>I am now implementing <code>MutableStateFlow</code> to store the cache value in Android MVVM architecture . The goal is, it could be fetched only at the first time to avoid much redundant network connection and it could be also updated when needed. Therefore, I have the following questions:</p>
  <ol>
  <li>How to reset the value and request fetch data again in the MutableStateFlow with the following code?</li>
  <li>Am I on a wrong track to use lazy to save the cache value?</li>
  </ol>
  <p><a href="https://i.stack.imgur.com/2yWZ5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2yWZ5.png" alt="enter image description here" /></a></p>
  <pre><code>class WeatherRepository {
      private val mDefaultDispatcher: IDefaultDispatcher by inject()
      private val scope by lazy { CoroutineScope(mDefaultDispatcher.io() + SupervisorJob()) }
      val cacheWeather by lazy {
          val flow = MutableStateFlow&lt;List&lt;Weather&gt;?&gt;(null)
          scope.launch(mDefaultDispatcher.io()) {
              val response = getWeather()
              if (response.isValid) {
                  val data = response.data
                  flow.value = data
              }
          }
          flow
      }
  }
  </code></pre>
  <pre><code>class ViewModelA {
      private val mRepository: WeatherRepository by inject()
      val weather by lazy {
          mRepository.cacheWeather.asLiveData()
      }
  
      fun requestUpdateOnWeather() {
         //TODO (dunno how to make the mutableStateFlow reset and fetch data again)
      }
  }
  </code></pre>
  <pre><code>class ViewModelB {
      private val mRepository: WeatherRepository by inject()
      val weather by lazy {
          mRepository.cacheWeather.asLiveData()
      }
  }
  </code></pre>
  <p>Appreciate any comment or advice</p>