Reputation: 772
My question is two fold. I had been using RxSearchView
for a while. I have used map
, filter
and finally switchMap
to query the search events from api. First in the map
or filter
function, I have updated the ui like this:
.filter(new Predicate<String>() {
@Override
public boolean test(String s) throws Exception {
if (searchViewQueryTextEvent.queryText().toString().length() < 3 && mUserList != null) {
mUserList.clear();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mUserSearchAdapter.notifyDataSetChanged();
}
});
}
return s.length() > 2;
}
})
So is it a good practice to keep updating the ui from inside these functions ? Secondly, deleting characters couldn't be handled this way. Please give me suggestions/ links. I am new to Rx. Thanks very much!
Upvotes: 0
Views: 212
Reputation: 281
So is it a good practice to keep updating the ui from inside these functions ?
You do not need to and should not use runOnUIThread()
here. You can add multiple calls to observeOn
operators within your reactive chain to switch threads. Here are some links on threading :
observeOn
and subscribeOn
: The subscribeOn and observeOn gifs on the blog make it easier to visualize the difference and useobserveOn
multiple times.Secondly, deleting characters couldn't be handled this way. Please give me suggestions/ links.
Instead of filtering you should instead use debounce()
operator.
Here's a link to my talk at Droidcon Boston, 2017 in which I talk about auto-search problem with debounce()
and switchMap()
Scroll to ~ 25:00 time. You can find the demo code which handles deletion of characters as well and also the blog post.
I hope this helps.
Upvotes: 1