1

I am using several filters on my Observable and I would like to report cases at the end of filtering when the result was empty. I cannot do it at the end of processing because this observable is supposed to be concatenated with another one:

        Observable.just(1, 2, 3)
                .concatWith(
                        Observable.just(2, 4, 6)
                                .filter(value -> ((value % 2) != 0))
                                // report if empty
                )
morisil
  • 1,345
  • 8
  • 19

2 Answers2

2

You can use switchIfEmpty and do something with this fallback Observable

 Observable.just(2, 4, 6)
            .filter(value -> ((value % 2) != 0))
            // replace the empty observable with an empty observable
            // but this observable will log when it will completed 
            .switchIfEmpty(Observable.<Integer>empty().doOnTerminate(() -> System.out.println("empty !")))
            .subscribe();
dwursteisen
  • 11,435
  • 2
  • 39
  • 36
1

You can use Transformers.doOnEmpty from rxjava-extras which is on Maven Central:

source.compose(Transformers.doOnEmpty(action))

You might use this solution if you cared about efficiency (allocations/performance) but otherwise use @dwursteisen's solution.

Dave Moten
  • 11,957
  • 2
  • 40
  • 47