title: Updating ViewModel Data?
body: <p>I have a UserViewModel that is responsible to fetch the user information and then I display it in the UI.</p>
  
  <p><strong>UserViewModel.java</strong></p>
  
  <pre><code>public class UserViewModel extends ViewModel {
  
  private MutableLiveData&lt;UserResponse&gt; userInfoMutableLiveData;
  
  public MutableLiveData&lt;UserResponse&gt; getUserInfoMutableLiveData() {
          if (userInfoMutableLiveData == null) {
              userInfoMutableLiveData = new MutableLiveData&lt;&gt;();
              fetchUserInfo();
          }
          return userInfoMutableLiveData;
      }
  
      private void fetchUserInfo() {
          // Asynchronous call using RxJava to fetch user information.
  }
  </code></pre>
  
  <p>I call it in the UI in the following way.</p>
  
  <p><strong>ProfileFragment.java</strong></p>
  
  <pre><code>@Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_profile, container, false);
  
      viewModel = ViewModelProviders.of(this, viewModelFactory).get(UserViewModel.class);
      viewModel.getUserInfoMutableLiveData().observe(this, this::consumeResponse);   
  
      return view;
  }
  </code></pre>
  
  <p>This displays the data correctly and it works fine. </p>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>When there is no internet connection, my app displays a screen with a no internet connection warning and then a button to retry the call. </p>
  
  <p><strong>ProfileFragment.java</strong></p>
  
  <pre><code>@Override
  public void onRetryClick() {
      viewModel.getUserInfoMutableLiveData().observe(this, this::consumeResponse);
  }
  </code></pre>
  
  <p>Suppose the following scenario, I make the call without an internet connection --> retry button is displayed --> I reconnect to the internet and then --> press retry button. </p>
  
  <p>It will keep giving me the error of no internet connection because it is just retrieving the previous <code>userInfoMutableLiveData</code> and it is not loading a new one.</p>
  
  <p><strong>WHAT I TRIED</strong></p>
  
  <p>I tried to fetch the user information in the UI like this. </p>
  
  <p><strong>ProfileFragment.java</strong></p>
  
  <pre><code>@Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_profile, container, false);
  
      viewModel = ViewModelProviders.of(this, viewModelFactory).get(UserViewModel.class);
      viewModel.getUserInfoMutableLiveData().observe(this, this::consumeResponse);   
      viewModel.fetchUserInfo();
  
      return view;
  }
  </code></pre>
  
  <p>and then call <code>onRetryClick()</code> like this.</p>
  
  <p><strong>ProfileFragment.java</strong></p>
  
  <pre><code>@Override
  public void onRetryClick() {
      viewModel.fetchUserInfo();
  }
  </code></pre>
  
  <p>It solves this problem because it will make a call each time I click the retry button, however, it lost the purpose of ViewModel that is to load the data once and then retrieve it.</p>
  
  <p><strong>What I need?</strong></p>
  
  <p>In conclusion, I need to correctly retry the ViewModel call in case there is no internet connection.</p>