I'm new with RxJava
and just trying to implement a few examples to better understand whats going on so I what I have thought is an EditText
which is an Observable and emits textChangeEvent
s using RxBinding
library :
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());
and a Retrofit
api call where the text for the call is from the Observable
above.
So I have declared a function :
@Override
public Observable<SearchResponse> executeSearch(Observable<RxTextView> queryText) {
return searchService.getSearch(queryText)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
}
And now I have to combine those two so when text changes there's a new Retrofit
call but I don't know how I can "pass" the queryText
to the service.
I also tried :
Observable<TextViewTextChangeEvent>
searchBarText = RxTextView.textChangeEvents(searchbar.getEditText())
.debounce(400, TimeUnit.MILLISECONDS)
.filter(new Func1<TextViewTextChangeEvent, Boolean>() {
@Override
public Boolean call(TextViewTextChangeEvent text) {
Timber.i("Executes!!! text : %s", text.text().toString());
return (text.text().length() > 2);
}
})
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io());
Observable.combineLatest(executeSearchTypeOne(searchBarText), executeSearchTypeTwo(searchBarText),
new Func2<TypeOne, TypeTwo, Object>() {
@Override
public Object call(TypeOne one, TypeTwo two) {
return null;
}
})
.subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
Timber.i("WORKS!!!");
}
});
where I want to run two Retrofit
calls concurrently based on EditText
changes but they don't run at all
I assume searchService.getSearch queryText parameter's type is String, and return an Observable of Data.
I would write it that way:
RxTextView.textChangeEvents(searchbar.getEditText())
.debounce(400, TimeUnit.MILLISECONDS)
.map(new Func1<TextViewTextChangeEvent, String>() {
@Override
public String call(TextViewTextChangeEvent text) {
return text.text().toString();
}
})
.filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String text) {
return (text.length() > 2);
}
})
.flatMap(new Func1<String, rx.Observable<Data>>() {
@Override
public rx.Observable<Data> call(String text) {
return getSearch(text);
}
})
.subscribeOn(Schedulers.io()) // Or Schedulers.newThread()
.observeOn(AndroidSchedulers.mainThread());
This is an Observable of Data, from an Observable of TextViewTextChangeEvent mapped to String to chain with getSearch, an Observable Data.
For Kotlin (initial proposal by Killer):
RxTextView.textChangeEvents(searchbar.getEditText())
.debounce(400, TimeUnit.MILLISECONDS) // Better store the value in a constant like Constant.DEBOUNCE_SEARCH_REQUEST_TIMEOUT
.map { it.text().toString() }
.filter { it.length > 2 }
.flatMap { getSearch(it) } // Or .flatMap(this::getSearch)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { ... }
Note: RxTextView.textChanges will not emit text changes once onError() method is run. Reference: https://github.com/JakeWharton/RxBinding/issues/272
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With