java 如果它是空的,则切换 observable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37545982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:37:09  来源:igfitidea点击:

Switch observable if it is empty

javarealmrx-java

提问by Ignacio Giagante

I implemented two repository in order to manage my data. So, if there are not data in database, it should ask to the API about it. I saw in other post that this could be resolved using switchIfEmpty, but it didn't work for me.

我实现了两个存储库来管理我的数据。因此,如果数据库中没有数据,则应向 API 询问。我在其他帖子中看到这可以使用switchIfEmpty解决,但它对我不起作用。

I tried the following code. The line restApiFlavorRepository.query(specification)is called, but the subscriber is never notified.

我尝试了以下代码。调用了restApiFlavorRepository.query(specification)行,但永远不会通知订阅者。

public Observable query(Specification specification) {

    final Observable observable = flavorDaoRepository.query(specification);

    return observable.map(new Func1() {
        @Override
        public Observable<List<Flavor>> call(Object o) {
            if(((ArrayList<Flavor>)o).isEmpty()) {
                return restApiFlavorRepository.query(specification);
            }
            return null;
        }
    });

}

and this

还有这个

public Observable query(Specification specification) {

    final Observable observable = flavorDaoRepository.query(specification);

    return observable.switchIfEmpty(restApiFlavorRepository.query(specification));

}

And I'm still getting empty list, when I should obtain two Flavors.

当我应该获得两种口味时,我仍然得到空清单。

UPDATED

更新

What I was looking for, was this...

我要找的,是这个……

public Observable query(Specification specification) {

    Observable<List<Plant>> query = mRepositories.get(0).query(specification);

    List<Plant> list = new ArrayList<>();
    query.subscribe(plants -> list.addAll(plants));

    Observable<List<Plant>> observable = Observable.just(list);

    return observable.map(v -> !v.isEmpty()).firstOrDefault(false)
            .flatMap(exists -> exists
                    ? observable
                    : mRepositories.get(1).query(null));
}

and it works like charm!! :)

它的作用就像魅力一样!!:)

回答by akarnokd

The switchIfEmpty()requires the source to complete without any values in order to switch to the second source:

switchIfEmpty()要求源,以便没有任何值,完成切换到第二源:

Observable.empty().switchIfEmpty(Observable.just(1))
.subscribe(System.out::println);

This one won't switch:

这个不会切换:

Observable.just(new ArrayList<Integer>())
.switchIfEmpty(Observable.just(Arrays.asList(2)))
.subscribe(System.out::println);

If you want to switch on a 'custom' notion of emptiness, you can use filter:

如果你想打开一个“自定义”的空性概念,你可以使用filter

Observable.just(new ArrayList<Integer>())
.filter(v -> !v.isEmpty())
.switchIfEmpty(Observable.just(Arrays.asList(2)))
.subscribe(System.out::println);