I'd like to use the same LiveData with different sources. One from an API call which is an observable and one from a database which is a LiveData. I'd like to be able to do something like this:
private LiveData<List<Items>> items = new MutableLiveData<>();
// this one comes from an API and it's an observable
public void onApiItemsSelected(String name) {
Disposable disposable = repository.getItemsFromApi(name)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(itemsList -> {
items.setValue(itemsList);
});
compositeDisposable.add(disposable);
}
// this one comes from the database and it's a livedata that I want to transform first
public void onDatabaseItemsSelected() {
items = Transformations.map(repository.getItemsFromdatabase(), itemsList -> {
List<Items> finalItemsList = new ArrayList<>();
for (Item item : itemsList) {
finalItemsList.add(itemsList.toSomething());
}
return finalItemsList;
});
}
The problem is that a Transformation.map always returns a LiveData and in order to make a items.setValue(itemsList) items need to be a MutableLiveData. I tried with MediatorLiveData but it's calling the two sources and mixing everything. That's not what I need here. I need one source OR the other. Is it possible?