I need to perform multiple http requests and a database query in order to filter data.
I am usin the zip operator from Rxjava
I did the following to prepare my multiple http requests
List<Observable<List<Picture>>> requests = new ArrayList<>();
for(String id : photoIds) {
requests.add(pictureRepository.searchPicture(id).toObservable());
}
I did the following to prepare my database request
Observable<List<Favourite>> favourites = pictureRepository.getFavourites().toObservable();
I try to create the observable but rxjava is not accepting my code
Observable.zip(
requests, favourites,
new BiFunction<Object[], List<Favourite>, List<FavouritePictures>>() {
@Override
public List<FavouritePictures> apply(Object[] t1, List<Favourite> t2) throws Exception {
return /*here I want to check the favourites and return a list*/;
}
}
);
Is there a way to achieve this? Thanks
A solution that may work for this is the following:
val photoIds = listOf(1, 2, 3)
var index = 0
Observable.just(0).flatMapSingle {
pictureRepository.searchPicture(photoIds[index])
index += 1
}.repeatUntil {
index + 1 == photoIds.size - 1
}.toList().zipWith(pictureRepository.getFavourites(),
BiFunction { listOfPictures, listOfFavoratePictures ->
// Return your list
})
You could also achieve this using a recursive method but I personally find this more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With