我有以下代码,可以很好地在列表视图中显示结果。问题是该调用在键入时会阻止键盘,但是当我尝试在io线程上订阅它时它不起作用。我的问题是:拨打我的休息界面的正确位置在哪里?
o1 = RxTextView.textChangeEvents(etSearch).throttleLast(500, TimeUnit.MILLISECONDS)
.map(new Func1<RxTextViewTextChangeEvent, String>() {
@Override
public String call(RxTextViewTextChangeEvent rxTextViewTextChangeEvent) {
return etSearch.getText().toString();
}
}
);
o1.observeOn(RxAndroidSchedulers.mainThread()).filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
return s.length() > 2;
}
}).subscribe(new Action1<String>() {
@Override
public void call(String s) {
List<CrewMember> crewMemberList = rest.getCrewMemberList(s);
ListView crewFinderResultsListView = (ListView) findViewById(R.id.list);
crewMemberAdapter = new CrewFinderResultRowAdapter(mContext, getLayoutInflater());
crewMemberAdapter.setCrewMemberList(crewMemberList);
crewFinderResultsListView.setAdapter(crewMemberAdapter);
}
});
答案 0 :(得分:1)
我的猜测是rest.getCrewMemberList(s)
是一个网络调用或其他类型的阻塞调用,阻止了主线程。如果是第一个,你可能会在主线程异常上获得一个被吞下的网络,因为你没有使用带有错误回调的subscribe
重载。您必须将调用移动到io()调度程序:
RxTextView.textChangeEvents(etSearch)
.throttleLast(500, SECONDS)
.filter(s -> s.length() > 2)
.switchMap(s ->
Observable.fromCallable(() -> rest.getCrewMemberList(s))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(crewMembersList -> {
ListView crewFinderResultsListView = (ListView) findViewById(R.id.list);
crewMemberAdapter = new CrewFinderResultRowAdapter(
mContext, getLayoutInflater());
crewMemberAdapter.setCrewMemberList(crewMemberList);
crewFinderResultsListView.setAdapter(crewMemberAdapter);
},
e -> Log.d(...));