GeoDataClient.getAutocompletePredictions - Canceling/Ignoring 之前的请求

GeoDataClient.getAutocompletePredictions - Canceling/Ignoring Previous Requests

Android SDK https://developers.google.com/places/android-sdk/autocomplete (以编程方式查找获取地点预测)

GeoDataClient.getAutocompletePredictions() - 我想忽略不是最后请求的响应... 自动完成输入例如: 'New'->'New Y'->'New Yo'

3 条回复 - 但我只想看到最后一条.. (不使用 RX)

// Submit the query to the autocomplete API and retrieve a PendingResult that will
   // contain the results when the query completes.
   Task<AutocompletePredictionBufferResponse> results =
           geoDataClient.getAutocompletePredictions(constraint, bounds, typeFilter);
   results.addOnSuccessListener(autocompletePredictions -> {
       if (autoCompletePredictionsListener != null) {
           autoCompletePredictionsListener.onAutoCompleteSuccess(autocompletePredictions);
       }
//****Here I want to ignore(or cancel somewhere before) previous requests
       autocompletePredictions.release();
   });

iOS SDK - 由 Google 开发人员 解决 https://developers.google.com/places/ios-sdk/reference/interface_g_m_s_autocomplete_fetcher

如果这些预测是针对最近一次对 sourceTextHasChanged 的​​调用中提供的文本,则只会使用预测结果调用委托。

最近有类似的需求,这就是我为达到预期结果所做的

// create a class-scope variable to track the most recent query
private String lastQuery;
private GeoDataClient geoDataClient;

// wrap the geoDataClient.getAutocompletePredictions in a class to associate the prediction results with the query that triggered the call
class AutocompletePredictor {
    String query;

    AutocompletePredictor(String query) {
        this.query = query;
    }

    Task<AutocompletePredictionBufferResponse> getPredictions(LatLngBounds bounds, AutocompleteFilter typeFilter) {
        return geoDataClient.getAutocompletePredictions(query, bounds, typeFilter);
    }
}

// modify your method that triggers the autocomplete filter
void filterAutocomplete(String constraint) {
    // update lastQuery every time this method is called
    lastQuery = constraint;

    // Submit the query to the autocomplete API and retrieve a PendingResult that will contain the results when the query completes.
    final AutocompletePredictor predictor = new AutocompletePredictor(constraint);
    Task<AutocompletePredictionBufferResponse> results = predictor.getPredictions(bounds, typeFilter);

    results.addOnSuccessListener(autocompletePredictions -> {
        // checks if the query for this filter is same as the most recent query issued to this method
        if (autoCompletePredictionsListener != null && predictor.query.equals(lastQuery)) {
            autoCompletePredictionsListener.onAutoCompleteSuccess(autocompletePredictions);
        }

        autocompletePredictions.release();
    });
}

编辑: 用户输入时延迟调用...
与其在每次 EditText 的内容更改时(可以是每次用户键入字符时)调用自动完成方法,不如安排自动完成调用在实际执行之前等待一段时间。如果在等待时间结束前,EditText内容再次发生变化,则取消之前的调度,重新调度。

editText.addTextChangedListener(new TextWatcher() {
    int delayMilliseconds = 500;
    Handler handler = new Handler();

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    @Override
    public void afterTextChanged(Editable editable) {
        final String constraint = editable.toString();

        // remove all delayed/pending tasks set in the last 500 milliseconds
        handler.removeCallbacksAndMessages(null);

        // setup a new delayed task to execute after 500 milliseconds
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                filterAutocomplete(constraint);
            }
        }, delayMilliseconds);
    }
});

不好,因为你所做的只是过滤结果显示,但实际上,所有geoDataClient任务最终都是异步完成的,大大减少了使用配额。 Handler 部分解决了这个问题。

所以我们需要一些其他方法来取消 geoDataClient.getAutocompletePrediction 任务,以一种常规 API 调用可以做到的方式。