简体   繁体   中英

Is making network call safe in ArrayAdapter?

I have developed AutoCompleteTextView with Google Places API. When the user enters address, I'm making network call to API. I tried to simulate crash, but the request completes before I'm able to change the configuration.

    public class PlacesAutoCompleteAdapter extends ArrayAdapter<Prediction> implements Filterable {

    private List<Prediction> resultList;

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public Prediction getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    ShlepenApp.getGoogleRestClient().getPlaceList(GoogleService.API_KEY, constraint.toString(), new Callback<GooglePlacesListResponse>() {
                        @Override
                        public void success(GooglePlacesListResponse placesListResponse, Response response) {
                            Log.i("TAGE", "SUCCESS");
                            resultList = placesListResponse.getPredictions();
                            notifyDataSetChanged();
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            Log.i("TAGE", "FAILURE");
                            //TODO: Post something useful here.

                        }
                    });
                    // 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;
    }
}

EDIT: Can you please elaborate what actually happens during a network call when I rotate the device? Will this call be lost, and then recreated because letters are present in autotextview?

Can you please elaborate what actually happens during a network call when I rotate the device? Will this call be lost, and then recreated because letters are present in autotextview?

I have not worked with Google Places API but I think this may help you:

when you rotate your device your activity is killed and your adapter but the thread that is doing network operation is exist and doing its job. After it has finished its job it is going to tell you the result(call one of the callback failure or success) but there is no adapter because the old one is garbage collected and the thread dose not access to the new adapter so it is calling callback method and in each of the method it is going to access the null object (private List<Prediction> resultList;) .

As per the documentation the method Filter.performFiltering() is invoked in a worked thread, therefore you won't block the UI by doing your IO operation inside this method.

Source: http://developer.android.com/reference/android/widget/Filter.html#performFiltering(java.lang.CharSequence)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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