Fallback Observable for RxJava

前端 未结 6 1430
-上瘾入骨i
-上瘾入骨i 2021-02-02 00:25

I\'m in search for a better way to achieve a simple Observable fallback system for empty results when using RxJava. The idea is that, if a local query for a set of data results

6条回答
  •  野的像风
    2021-02-02 01:10

    After some research and looking at other peoples responses, I believe using a Transformer is the most robust solution, like so...

    Observable.from(Collections.emptyList())
      .compose(new Observable.Transformer, List>() {
        @Override
        public Observable call(Observable> source) {
          boolean isEmpty = observable.isEmpty().toBlocking().first();
          if (isEmpty) {
            return backupObservable();
          } else {
            return source;
          }
        }
      });
    
        

    提交回复
    热议问题