Search code examples
javarx-javarx-android

RxJava filter and emit other items


Its possible to filter and continue emiting itens like below ?


My code that calls subscriber 2 times:

Observable<Map.Entry<String, ArrayList<MockOverview>>> requestEntries =
        this.requestView.request(request)
        .map(HashMap::entrySet)
        .flatMapIterable(entries -> entries);

requestEntries.filter(entry -> entry.getKey().equals("featured"))
        .map((Func1<Map.Entry<String, ArrayList<MockOverview>>, List<MockOverview>>) Map.Entry::getValue)
        .subscribe(mockOverviews -> {
            Log.i("subscrive", "featured");
        });

requestEntries.filter(entry -> entry.getKey().equals("done"))
        .map((Func1<Map.Entry<String, ArrayList<MockOverview>>, List<MockOverview>>) Map.Entry::getValue)
        .subscribe(mockOverviews -> {
            Log.i("subscrive", "featured");
        });

What i want:

 requestEntries.filter(entry -> entry.getKey().equals("featured"))
        .map((Func1<Map.Entry<String, ArrayList<MockOverview>>, List<MockOverview>>) Map.Entry::getValue)
        .subscribe(mockOverviews -> {

        })
        .filter(entry -> entry.getKey().equals("done"))
        .map((Func1<Map.Entry<String, ArrayList<MockOverview>>, List<MockOverview>>) Map.Entry::getValue)
        .subscribe(mockOverviews -> {

        });

Solution

  • You can use doOnNext in the place of the first subscribe()

     requestEntry.filter(v -> ...)
     .map(v -> ...)
     .doOnNext(v -> ...)
     .filter(v -> ...)
     .map(v -> ...)
     .subscribe(...)
    

    or use publish(Func1):

     requestEntry.filter(v -> ...)
     .map(v -> ...)
     .publish(o -> {
         o.subscribe(...);
         return o;
     })
     .filter(v -> ...)
     .map(v -> ...)
     .subscribe(...)