简体   繁体   English

数据库为空时RxJava调用网络

[英]RxJava call the network when database is empty

I'm using RxJava, Room and Retrofit to accomplish Repository pattern in Android. 我正在使用RxJava,Room和Retrofit在Android中完成存储库模式。 Everything is working except this one thing. 除了这一件事,一切都在工作。 I have implemented SearchView for searching data. 我已经实现了SearchView来搜索数据。 Now when search view text changes I call search method. 现在,当搜索视图文本更改时,我将调用搜索方法。 here is how it looks like 这是它的样子

mLocal.searchMovies(query) //returns Single
mRemote.searchMovies(query) //returns Single

public Single<List<MovieEntity>> search(String query) {
    return mLocal.searchMovies(query)
        .toMaybe()
        .switchIfEmpty(mRemote.searchMovies(query)
        .map(data -> {
            mLocal.saveAll(data);
            return data;
        }));
}

mLocal queries the room database and mRemote calls retrofit to fetch data from REST API. mLocal查询会议室数据库,而mRemote调用改造以从REST API中获取数据。 The problem is, this only calls mLocal and when room returns empty row network call is not initiated. 问题是,这仅调用mLocal,并且当房间返回空行时不会启动网络调用。 I have tried everything I possibly and read many articles but I cannot understand how to get it work. 我已经尝试了所有可能的方法,并阅读了许多文章,但是我不明白如何使它起作用。

Each of mLocal and mRemote works fine. mLocal和mRemote都可以正常工作。 Only chaining them does not accomplish what I want. 仅链接它们并不能实现我想要的功能。

Your chain does not work because .toMaybe() from Single cannot produce Maybe.empty() , which is the result required to have .switchIfEmpty take effect. 您链不工作,因为.toMaybe()Single不能产生Maybe.empty()这是有要求的结果.switchIfEmpty生效。

You will either need to replace it with Single.onErrorResume so the error condition from a missing entry results in the remote query, or make it return Maybe to accurately represent the tristate of <Result,NotCached,Error> . 您可能需要用Single.onErrorResume替换它,以便缺少条目的错误条件导致远程查询,或者使其返回Maybe以准确表示<Result,NotCached,Error>的三态。

The explanation of my problem is the same as in this answer . 我的问题的解释与此答案相同。 I will copy relevant part and post my working piece of code after applying it. 应用后,我将复制相关部分并发布我的工作代码段。

Note: I changed to Flowable from Single/MayBe. 注意:我从Single / MayBe更改为Flowable。 am not sure how much it affects the original code, hadn't I changed the type of Observables. 不知道它对原始代码有多大影响,是否没有更改Observables的类型。

Based on the OP's feedback, the allDatas source was infinite but returned an empty List. 根据OP的反馈,allDatas源是无限的,但返回了一个空的List。 The resolution was to apply at least take(1) to get exactly one response from allDatas and then optionally filter out the empty List so switchIfEmpty can switch over to the alternatives 解决方案是至少应用take(1)从allDatas中获得一个准确的响应,然后选择过滤空列表,以便switchIfEmpty可以切换到其他方法

public Flowable<List<MovieEntity>> getMovies() {
        return mLocal.allMovies().take(1)
                .filter(list -> !list.isEmpty())
                .switchIfEmpty(mRemote.allMovies()
                                .doOnNext(data -> mLocal.saveAll(data))
                        );
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM