我有一个ViewPager
,在它下面有一个Retrofit
中有四个标签,我目前正在做的是在文本更改时使用 RxTextView.textChangeEvents(searchbar.getEditText())
.debounce(400, TimeUnit.MILLISECONDS)
.filter(new Func1<TextViewTextChangeEvent, Boolean>() {
@Override
public Boolean call(TextViewTextChangeEvent text) {
return (text.text().length() > 2);
}
})
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
.switchMap(new Func1<TextViewTextChangeEvent, Observable<?>>() {
@Override
public Observable<?> call(TextViewTextChangeEvent textViewTextChangeEvent) {
String searchBarText = textViewTextChangeEvent.text().toString();
switch (visibleTab) {
case TAGS:
return presenter.executeSearchPostsByTag(searchBarText, String.valueOf(0));
case PEOPLE:
return presenter.executeSearchPostsByPeople(searchBarText, String.valueOf(0));
case COMPANIES:
return presenter.executeSearchPostsByCompanies(searchBarText, String.valueOf(0));
case JOBS:
return presenter.executeSearchPostsByJobs(searchBarText, String.valueOf(0));
default:
return presenter.executeSearchPostsByTag(searchBarText, String.valueOf(0))
.toCompletable().toObservable();
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Object>() {
// here I use EventBus to pass the results to the Fragments (Tabs)
// ... code omitted ...
向Rest服务执行请求
EditText
从每个标签的代码中可以看出,我有不同的请求。
但现在需求已经发生变化,对于combineLatest' so I added all four calls to
中的每个文本更改,我必须同时执行所有四个请求。
我搜索了很多,我发现的是我可以使用 Observable.combineLatest(presenter.executeSearchPostsByTag( /** what to add as queryText **/ ),
presenter.executeSearchPostsByJobs(/** what to add here as queryText**/), //omits the other two calls)
combineLatest`,但由于它们都依赖于文本更改,所以没有发生任何事情。
queryText
如上所示,我不知道如何通过combineLatest
。必须有一种方法将RxTextView.textChangeEvents(searchbar.getEditText())...
代码与switchMap
代码相结合,直到我在开头发布的combineLatest
但我找不到它。
编辑:也许 .map(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
presenter.executeSearchPostsByTag(s, String.valueOf(0));
presenter.executeSearchPostsByPeople(s, String.valueOf(0));
presenter.executeSearchPostsByJobs(s, String.valueOf(0));
presenter.executeSearchPostsByCompanies(s, String.valueOf(0));
return true;
}
})
错了,我可以做类似的事情:
OPENROWSET
但后来我不知道如何将结果发送给订阅者
答案 0 :(得分:1)
你很亲密。只需将开关地图替换为:
即可.flatMap(new Func1<TextViewTextChangeEvent, Observable<CombinedData>>() {
@Override
public Observable<CombinedData> call(TextViewTextChangeEvent textViewTextChangeEvent) {
String searchBarText = textViewTextChangeEvent.text().toString();
return Observable.combineLatest(
presenter.executeSearchPostsByTag(searchBarText, String.valueOf(0)),
presenter.executeSearchPostsByPeople(searchBarText, String.valueOf(0)),
...);
}
})
为了更好的可读性,请创建一个提取组合部分的方法。类似的东西:
Observable<CombinedData> fetchData(String searchBarText) {
return Observable.combineLatest(
presenter.executeSearchPostsByTag(searchBarText, String.valueOf(0)),
presenter.executeSearchPostsByPeople(searchBarText, String.valueOf(0)),
...);
}
但如果可以的话,最好将它分成四部分。声明一个Observable:
Observable<TextViewTextChangeEvent> requestObservable = RxTextView.textChangeEvents(searchbar.getEditText())
.debounce(400, TimeUnit.MILLISECONDS)
.filter(new Func1<TextViewTextChangeEvent, Boolean>() {
@Override
public Boolean call(TextViewTextChangeEvent text) {
return (text.text().length() > 2);
}
})
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io());
然后,您为每个请求订阅四次:
.flatMap(new Func1<TextViewTextChangeEvent, Observable<Post>>() {
@Override
public Observable<Post> call(TextViewTextChangeEvent textViewTextChangeEvent) {
String searchBarText = textViewTextChangeEvent.text().toString();
return presenter.executeSearchPostsByTag(searchBarText, String.valueOf(0));
}
})
.subscribe(...