14

I am using LiveData in AndroidViewModel class where i need to wait for the response, but there are some cases where i need to check some value from local shared preference, which won't block any thread.

For these scenarios i am not using LiveData. This is what i am doing in my activity class.

 homeViewModel.sendTokenToServer().observe(this, isFCMSendToServer -> {
        Toast.makeText(this, "FCM Token Observer called", Toast.LENGTH_SHORT).show();
 });


//Without Live Data

if(homeViewModel.isUpgradeAvailable()){
     displayAlertMessage();
}

I want to know if the above approach is fine, or we have to use LiveData or some other observers for each method in ViewModel

dev90
  • 7,187
  • 15
  • 80
  • 153
  • 1
    `LiveData` can be part of your Synchronous and Asynchronous call. No need to switch to the typical listener pattern. – Enzokie May 23 '18 at 08:44

1 Answers1

11

AFAIK ViewModel and LiveData are not tightly connected. Therefore you are not obliged to use any observers. For example in this sample app, the ViewModel is used to preserve some numbers through orientation change. Therefore ViewModel serves as a container (something like a headless fragment) where you can store data to be retained through orientation change.

Suleyman
  • 2,765
  • 2
  • 18
  • 31