简体   繁体   English

Google Places自动完成API-线程

[英]Google Places Autocomplete API - threading

I have implemented autocomplete of Google Places using the API following this tutorial pretty strictly - https://developers.google.com/places/training/autocomplete-android . 我已经严格按照本教程使用API​​来实现Google Places的自动填充功能-https: //developers.google.com/places/training/autocomplete-android My question is, why is this working when no multi-threading work has been done - no asynchTasks or anything. 我的问题是,当没有完成多线程工作时-为什么没有asynchTasks或其他任何东西,为什么这样做有效? Shouldn't the app be crashing, as there is an http request from the main thread? 主线程发出http请求,应用程序是否应该崩溃? Here'e the line that I would think the app would crash at - 这是我认为该应用程序会崩溃的行-

conn = (HttpURLConnection) url.openConnection();

The network call is performed inside the Filter.performFiltering method: 网络调用在Filter.performFiltering方法内部执行:

@Override
public Filter getFilter() {
    Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            if (constraint != null) {
                // Retrieve the autocomplete results.
                resultList = autocomplete(constraint.toString());

                // Assign the data to the FilterResults
                filterResults.values = resultList;
                filterResults.count = resultList.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            }
            else {
                notifyDataSetInvalidated();
            }
        }};
    return filter;
}

As you can see from the documentation, the Filter.performFiltering() method is executed inside a worker thread: 从文档中可以看到, Filter.performFiltering()方法是在工作线程内执行的:

Invoked in a worker thread to filter the data according to the constraint. 在工作线程中调用以根据约束条件过滤数据。 Subclasses must implement this method to perform the filtering operation. 子类必须实现此方法才能执行过滤操作。 Results computed by the filtering operation must be returned as a Filter.FilterResults that will then be published in the UI thread through publishResults(CharSequence, android.widget.Filter.FilterResults) . 过滤操作所计算的结果必须作为Filter.FilterResults返回,然后将通过publishResults(CharSequence, android.widget.Filter.FilterResults)在UI线程中发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM