我如何在存储库中合并过滤器领域结果和网络搜索

时间:2018-07-24 05:49:22

标签: java android realm rx-java2

我想要合并领域结果和网络搜索,并在UI上观察该结果。

下面的代码在脱机使用时效果很好,但是当来自网络的更新数据时 ,Realm Auto Refresh不会考虑上一次搜索查询,只是找到所有结果。

达到这个目标的更好的模式是什么?

rx_search视图

 @Override
public void search(Observable<CharSequence> searchObservable) {
    isSearching = true;
    Disposable subscribe = searchObservable
            .map(CharSequence::toString)
            .filter(searchViewQueryTextEvent ->
                    searchViewQueryTextEvent.length() > 1).distinctUntilChanged()
            .flatMap(s -> {
                    String[] s1=s.trim().split(" ");
                    String s2="";
                    for (String e:s1){
                        s2=s2+e+"*";
                    }

               return Observable.just(s2);
                })
              //switchMap to get result from repository
            .switchMap(s -> mInteractor.searchStores(s).onBackpressureLatest().toObservable())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(stores -> {
                        if (mView != null) {
                            mView.setList(stores);
                        }
                    }, Throwable::printStackTrace
                    , () -> {
                    }, d -> disposable.add(d));
    disposable.add(subscribe);

从领域获取结果并从net更新数据库:

@Override
public Flowable<RealmResults<Stores>> searchStores(String searchString) {
    searchNetwork(searchString); //  after update from net realm emit all result table
    try (Realm realm = Realm.getDefaultInstance()) {
         //Query work for first time and after update data  flowble 
         //emit all item and Does not consider  query
        RealmQuery<Stores> query = realm.where(Stores.class).like("name", searchString, Case.INSENSITIVE);
        return query
                .findAll()
                .asFlowable();

    }
}
  private void searchNetwork(CharSequence searchString) {
    api.searchList(searchString)
            .doOnError(Throwable::printStackTrace)
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.io())
            .filter(statusPojo::getStatus)
            .switchMap(data -> Observable.just(data.getData()))
            .subscribe(new DisposableObserver<List<Stores>>(){

                @Override
                public void onNext(List<Stores> stores) {
                    try (Realm realm = Realm.getDefaultInstance()) {
                        realm.executeTransaction(r -> r.insertOrUpdate(stores);
                    }
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onComplete() {
                }
            })
    ;
}

1 个答案:

答案 0 :(得分:0)

我每次使用领域可变对象发出更改结果及其工作时都使用switchMap设置查询:

@Override
public Flowable<RealmResults<Stores>> searchStores(String searchString) {
searchNetwork(searchString);

 try (Realm realm = Realm.getDefaultInstance()) {

    RealmQuery<Stores> query = realm.where(Stores.class).like("name", searchString, 
    Case.INSENSITIVE);
    return query
            .findAll()
            .asFlowable()
            .onBackpressureLatest()
            .flatMap(realmResultsCollectionChange -> 
                      query.findAll().asFlowable());

}
}