switchMapSingle() 不会取消之前的请求
switchMapSingle() doesn't cancel previous request
我的数据库 table 'order' 有 6000 行,我想使用 switchmap 按数字在其中创建搜索以取消之前的请求。但这是行不通的,它总是 return 先前请求的结果。我的代码有什么问题?
示例: 输入 1..0..7 returns 107%,然后是 10%,然后是 1%。它应该 return 只有 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: 添加了获取 OrderParameter
在 RxBinding 的帮助下,在 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())));
我的数据库 table 'order' 有 6000 行,我想使用 switchmap 按数字在其中创建搜索以取消之前的请求。但这是行不通的,它总是 return 先前请求的结果。我的代码有什么问题?
示例: 输入 1..0..7 returns 107%,然后是 10%,然后是 1%。它应该 return 只有 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: 添加了获取 OrderParameter
在 RxBinding 的帮助下,在 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())));