LiveData.getValue() returns null with Room
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Java POJO Object , I solve this problem through this approach private MediatorLiveData<List<Section>> mSectionLive = new MediatorLiveData<>();
.
.
.
@Override
public LiveData<List<Section>> getAllSections() {
final LiveData<List<Section>> sections = mDb.sectionDAO().getAllSections();
mSectionLive.addSource(sections, new Observer<List<Section>>() {
@Override
public void onChanged(@Nullable List<Section> sectionList) {
if(sectionList == null || sectionList.isEmpty()) {
// Fetch data from API
}else{
mSectionLive.removeSource(sections);
mSectionLive.setValue(sectionList);
}
}
});
return mSectionLive;
}
|
Android MVVM, can you observe LiveData inside an ViewModel with another LiveData object?
Date : March 29 2020, 07:55 AM
Hope this helps In my application I use this ViewModel: , You can use Transformations.map() val messages = MutableLiveData<List<Message>>()
val hasMessages: LiveData<Boolean> = Transformations.map(messages) {
it.isNotEmpty()
}
|
LiveData + ViewModel + Room: Exposing a LiveData returned by query which changes over time (Through a fts search)
Tag : android , By : Adrian Codrington
Date : March 29 2020, 07:55 AM
I wish this help you I have an FTS query in my DAO which I'd like to use to provide search in my App. The activity passes the query to view model each time the search text is changed. val dataSet :LiveData<List<WordMinimal>>
val searchQuery = MutableLiveData<String>()
init {
dataSet = Transformations.switchMap(searchQuery) { query ->
if (query == null || query.length == 0) {
//return WordRepository.getAllWords()
} else {
//return WordRepository.search(query)
}
}
}
fun setSearchQuery(searchedText: String) {
searchQuery.value = searchedText
}
|
How to test a method in viewModel which returns LiveData value from repository
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , As the question is tagged as unit-testing, IMHO, expecting real data from a web service does not actually fall into the scope of unit-testing. You might call than an integration testing, however, from the unit-testing point of view, you might consider mocking the response from the function that calls the web service and verify if the method was called using proper arguments that you expect.
|
How can LiveData in ViewModel observe the Livedata in Repository using Transformations?
Date : March 29 2020, 07:55 AM
With these it helps You can ignore my other answer. The solution is using MediatorLiveData<> in your view model. You add the LiveData from the repository as a data source to the MediatorLiveData<> and call setValue or postValue (depends on if it is in the UI tread or not) in the observer onChanged callback. Like so currentTransaction.addSource(application.getFinanceRepository().getTransaction(id),
new Observer<Transaction>() {
@Override
public void onChanged(@Nullable Transaction transaction) {
//Updates the MediatorLiveData<> that you activity is observing
currentTransaction.setValue(transaction);
}
});
|