I have DB table 'order' with 6000 rows and I want to create searching in it by number using switchmap for canceling previous request. But it's not work, it always returns result of previous request. What is wrong with my code?
Example: enter 1..0..7 returns 107%, then 10%, then 1%. It should return only 107%
disposable.add(
Observable.just(orderParameter)
.debounce(1, TimeUnit.SECONDS)
.distinctUntilChanged()
.switchMapSingle(orderParameter1 -> orderInteractor.getSearchOrderList(orderParameter1))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(orders -> {
getView().showOrders(orders);
}, throwable -> Timber.e(throwable.getMessage())));
@Query("SELECT `order`.order_id as id, " +
"client.last_name || ' ' || SUBSTR(client.first_name,1,1) || '. ' || SUBSTR(client.middle_name,1,1) || '.' as client_name, " +
"`order`.status as status, " +
"agency.short_name as agency_name, " +
"`order`.creation_date as date, " +
"`order`.price as price " +
"FROM `order`, agency, client where " +
"`order`.status =:status and agency.id = `order`.agency_id and client.id = `order`.client_id " +
"and `order`.agency_id =:agencyId and `order`.order_id like :orderId")
Single<List<Order>> getOrdersByStatusAndOrderIdAndAgencyId(String status, String orderId, long agencyId);
public class OrderParameter {
public long searchNumber = -1;
public long agencyId = -1;
}
OrderParameter orderParameter = new OrderParameter();
try {
if (TextUtils.isEmpty(searchText.trim())) {
orderParameter.searchNumber = -1;
} else {
orderParameter.searchNumber = Long.parseLong(searchText);
}
} catch (Exception e) {
}
orderParameter.agencyId = agencyId;
UPD: Added getting an OrderParameter
With the help of RxBinding, try this in onCreate:
disposable.add(
RxView.clicks(textField).map(v -> {
String searchText = searchTextField.getText().toString();
OrderParameter orderParameter = new OrderParameter();
try {
if (TextUtils.isEmpty(searchText.trim())) {
orderParameter.searchNumber = -1;
} else {
orderParameter.searchNumber = Long.parseLong(searchText);
}
} catch (Exception e) {
}
orderParameter.agencyId = agencyId;
return orderParameter;
})
.debounce(1, TimeUnit.SECONDS)
.distinctUntilChanged()
.switchMapSingle(orderParameter1 ->
orderInteractor.getSearchOrderList(orderParameter1))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(orders -> {
getView().showOrders(orders);
}, throwable -> Timber.e(throwable.getMessage())));