I am working on a feature where I need to filter out the network response data based on local database data.
For example, my network layers return me a list of items and my database layer returns an observable list of ids. Now I want to only return those objects from network layer whose id matches anyone from the database layer response.
Below code fetches data from the network and saves the result to a database(cache).
factory.getRemoteDataStore()
.searchForVenues(query)
.toObservable()
.distinctUntilChanged()
.flatMap { venues ->
factory.getCacheDataStore()
.saveVenues(venues)
.andThen(Observable.just(venues))
}
I also have a method that returns a list of venues that needs to be filtered
factory.getCacheDataStore().getDislikedVenues()
Now, how do I extend the previous chain to use getDislikedVenues() Observable to filter them from the response of network response?
Sorry for such noob question, I really am struggling with this.
One way of doing this is
P.S.: As I understand,
factory.getRemoteDataStore().searchForVenues(query)
returnsSingle
orMaybe
. In that casedistinctUntilChanged()
don't work because it relies on multiple emission (onNext()
), butSingle
orMaybe
can emit only one result.