Let's say we have two objects of LiveData:
LiveData<List<Foo>> fooList;
LiveData<List<Bar>> barList;
And by some method (or the constructor) Foo can be converted to the Bar object. What is the best way to convert the first observable, which has the list of Foo objects to the observable with the list of Bar objects.
I know that it is possible to do this:
barList = Transformations.map(fooList, fooList1 -> {
List<Bar> barList = new ArrayList<>();
for (Foo foo: fooList1) {
barList.add(new Bar(foo));
}
return barList;
});
But isn't there a better way similar to the flatMap operator in RxJava, by which we make all of the necessary conversions with the items from the list on the fly instead of dealing with the lists themselves as in the example above?
You can create your own transformation, following the recipe shown in the source to
Transformations
. FWIW, this sample app demonstrates a customfilter()
implementation:Otherwise, no.
LiveData
is meant as a lightweight RxJava analogue that is lifecycle-aware. If you need lots of operators, you are better served using RxJava directly.