android - Rxjava Filtering the duplicate item in List -
i using rxjava , retrofit in app.here's setup of app.when app launched app make 2 request 1 database , other network api (using retrofit) , both request return observable<list<article>>
. did merged 2 observable. problem network return articles present in database. how filter out duplicate item list. here's code.
return observable.merge(datasource.getarticles(source), remotesource.getarticles(source)) .distinct();
so tried distinct operator it's not filtering articles out.here's output looks form db.
article1 article2 article3 article4
output network
article7 articke8 article3 article4
what want distinct list of article
assuming article
has proper equals
implementation,
you collect them set:
datasource.getarticles(source) .mergewith(remotesource.getarticles(source)) .collect(hashset::new, (set, list) -> set.addall(list))
or unroll each list , apply distinct
followed tolist
:
datasource.getarticles(source) .mergewith(remotesource.getarticles(source)) .flatmapiterable(v -> v) .distinct() .tolist()
Comments
Post a Comment