Rx SearchView需要以较低的优先级取消正在进行的请求 [英] Rx SearchView needs to cancel on going request with less priority

查看:159
本文介绍了Rx SearchView需要以较低的优先级取消正在进行的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

RxSearchView.queryTextChangeEvents

RxSearchView.queryTextChangeEvents

检测键入时搜索"的事件以及提交搜索时, ? SAYT是要获取建议,当您提交时,它会执行完整搜索.

to detect the events of "Search As You Type" and also when you submit a search,  SAYT is to get suggestions and when you do a submit, it executes a full search.

此刻我有两个问题.

当您键入内容并获得建议时,突然之间单击提交",它将执行完整搜索,但问题是,如果有正在进行的建议请求,则可能会出现这样的情况:它们不应该出现在屏幕上因为完整搜索具有优先权.

When you are typing, and getting suggestions, but suddenly you click submit then it executes the full search but the problem is that if there is an on going suggestion request there might be the case that they appear on screen when they should not as the full search has priority.

因此,如果完整搜索中有提交,我想从建议中取消(取消订阅)请求.

So I would like to cancel (unsubscribe) the request from the suggestions in case there is a submit on the full search.

如何用这段代码实现?

另一个问题是,当我在搜索视图中删除搜索词并变空时,它会清除适配器,但是在某些情况下,当我清除搜索文本时,仍然会有一个进行中的建议请求并进行设置结果,但结果很清楚,因此我想保证,如果用户清除了搜索视图,则会取消所有请求.

Another problem is that when I am deleting the search term in the search view and it gets empty, then it clears the adapter but there are cases when I clear the search text, there is still an on going suggestions request and it sets the results but it just clear, so I would like to guarantee that if the user clears the searchview, it cancels all the requests.

这是我正在使用的代码.

Here is the Code I am using.

 RxSearchView.queryTextChangeEvents(searchView)
                .skip(1)
                .throttleLast(100, TimeUnit.MILLISECONDS)
                .debounce(200, TimeUnit.MILLISECONDS)
                .onBackpressureLatest()
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new Func1<SearchViewQueryTextEvent, Boolean>() {
                    @Override
                    public Boolean call(SearchViewQueryTextEvent searchViewQueryTextEvent) {
                        final boolean empty = TextUtils.isEmpty(searchViewQueryTextEvent.queryText());
                        if (empty) {
                            //Dont show anything  clear adapter
                            clearAdapter();
                        }
                        return !empty;
                    }
                })
                .concatMap(new Func1<SearchViewQueryTextEvent, Observable<Object>>() {
                    @Override
                    public Observable<Object> call(SearchViewQueryTextEvent searchViewQueryTextEvent) {

                        String searchTerm = searchViewQueryTextEvent.queryText().toString();
                        boolean submitted = searchViewQueryTextEvent.isSubmitted();
                        //Hide RecyclerView
                        //Show loading indicator
                        showLoading();
                        Observable<Object> observable;
                        if (submitted) {
                            observable = getFullSearch(searchTerm);
                        } else {
                            observable = getSuggestionsSearch(searchTerm);
                        }
                        return observable
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .doOnCompleted(new Action0() {
                                    @Override
                                    public void call() {
                                        //Show RecyclerView
                                        //Hide loading indicator
                                        showContent();
                                    }
                                });
                    }
                })
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onNext(Object object) {
                        //Set data on recyclerview and notify change.
                        setData(object);
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                });
    }

推荐答案

您可能想尝试

You might want to try switchMap instead, it just uses the last emited value from the observable.

从文档中

通过将提供的函数应用于源Observable发出的每个项目(返回一个Observable),然后再发射这些Observable中最近发射的项目,来返回新的Observable. 如果上游的Observable和最后的内部Observable(如果有)均完成,则生成的Observable完成.如果上游的Observable发出onError信号,则内部的Observable不再订阅,并且错误按顺序传递.

Returns a new Observable by applying a function that you supply to each item emitted by the source Observable that returns an Observable, and then emitting the items emitted by the most recently emitted of these Observables. The resulting Observable completes if both the upstream Observable and the last inner Observable, if any, complete. If the upstream Observable signals an onError, the inner Observable is unsubscribed and the error delivered in-sequence.

这篇关于Rx SearchView需要以较低的优先级取消正在进行的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆