Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxAndroid filter Observable<List<Item>>

I use RxAndroid and I'd like to filter the result of an Observable<List<Item>>.

Here is the code :

Observable<List<Item>> observable = 
Observable.create((Observable.OnSubscribe<List<Item>>) subscriber -> {
        subscriber.onNext(ItemManager.getItems());
        subscriber.onCompleted();
    });

I would like to use .filter() to only get the valid items. Doing this would be perfect :

observable.filter(new Func1<Item, Boolean>() {
    @Override
        public Boolean call(Item item) {
            return item.isValid();
    }
});

However, .filter() forces me to implement new Func1() :

observable.filter(new Func1<List<Item>, Boolean>() {
        @Override
            public Boolean call(Item item) {
        }
    });

Does somebody know how to solve the problem?

Thank you in advance :)

like image 315
Valentin Avatar asked Sep 19 '16 06:09

Valentin


1 Answers

     Observable.defer(() -> Observable.just(ItemManager.getItems())
            .subscribeOn(Schedulers.io())
            .flatMapIterable(items -> items)
            .filter(item -> item.isValid())
            .toList();
like image 168
Geralt_Encore Avatar answered Sep 22 '22 14:09

Geralt_Encore