0

我有 6000 行的数据库表“订单”,我想使用 switchmap 按数字在其中创建搜索以取消先前的请求。但它不起作用,它总是返回先前请求的结果。我的代码有什么问题?

示例:输入 1..0..7 返回 107%,然后是 10%,然后是 1%。它应该只返回 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

4

1 回答 1

0

在 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())));
于 2021-06-22T12:42:52.513 回答