title: Observing ViewModel LiveData from fragments without intermediary transformation variable
body: <p>I'm following the MVVM architecture in my android app. (<a href="https://developer.android.com/jetpack/guide" rel="nofollow noreferrer">https://developer.android.com/jetpack/guide</a>). All of my repository function returns a <strong>NetworkBoundResource</strong> object. You can read more about this design implementation from the jetpack guide link, or view the example project from google <a href="https://github.com/android/architecture-components-samples/blob/88747993139224a4bb6dbe985adf652d557de621/GithubBrowserSample/app/src/main/java/com/android/example/github/repository/NetworkBoundResource.kt" rel="nofollow noreferrer">here</a></p>
  <p>In this particular problem, I'm trying to re-fetch information over the network when I pull down on the screen.</p>
  <p><strong>Current working implementation</strong></p>
  <p>Fragment listener (triggered when screen pulled down)</p>
  <pre class="lang-kotlin prettyprint-override"><code>binding.refresh.setOnRefreshListener {
      profileViewModel.refreshUserProfile()
  }
  </code></pre>
  <p>Then in my ViewModel I have a MutableLiveData that updates value on this function call</p>
  <pre class="lang-kotlin prettyprint-override"><code>private val _refresh = MutableLiveData&lt;Boolean&gt;()
  
  fun refreshUserProfile() {
      _refresh.value = true
  }
  </code></pre>
  <p>And when <code>_refresh.value</code> changes, it triggers <code>switchMap</code></p>
  <pre class="lang-kotlin prettyprint-override"><code>val currentUserProfile: LiveData&lt;Resource&lt;UserProfile&gt;&gt; = _refresh.switchMap {
      if (it) {
          repository.getCurrentUserProfile(_username.value.toString())
      } else {
          AbsentLiveData.create()
      }
  }
  </code></pre>
  <p>Repository function</p>
  <pre class="lang-kotlin prettyprint-override"><code>
  fun getCurrentUserProfile(username: String): LiveData&lt;Resource&lt;UserProfile&gt;&gt; {
      return object : NetworkBoundResource&lt;UserProfile, UserProfile&gt;(AppExecutors.getInstance()) {
          override fun saveCallResult(item: UserProfile) {
              userProfileDao.update(item)
          }
          override fun shouldFetch(data: UserProfile?): Boolean {
              return true
          }
          override fun loadFromDb(): LiveData&lt;UserProfile&gt; {
              return userProfileDao.load(username)
          }
          override fun createCall(): LiveData&lt;ApiResponse&lt;UserProfile&gt;&gt; {
              return networkService.userProfileService.getUserProfileAsync(
                  token,
                  username
              )
          }
      }.asLiveData()
  }
  </code></pre>
  <p>And then back at my fragment, I have an observer on this <code>currentUserProfile</code> ViewModel variable</p>
  <pre class="lang-kotlin prettyprint-override"><code>profileViewModel.currentUserProfile.observe(viewLifecycleOwner, { userProfileResult -&gt;
      when (userProfileResult.status) {...}
  })
  </code></pre>
  <p><strong>What I am trying to do</strong></p>
  <p>I would like to skip the step where I'm setting <code>_refresh.value = true</code>. And instead observe the repository function directly like this:</p>
  <pre class="lang-kotlin prettyprint-override"><code>val currentUserProfile: LiveData&lt;Resource&lt;UserProfile&gt;&gt; = 
      repository.getCurrentUserProfile(_username.value.toString())
  </code></pre>
  <p>But this implementation will not trigger the observer in the fragment.</p>
  <p><strong>Question</strong>
  I don't quite understand why I wasn't able to trigger observer with my second implementation. And I'm also unsure if my initial implementation is even optimal or correct. I would appreciate any sort of feedback especially if you are familiar with this type of design.</p>