Skip to content

Commit

Permalink
Added cancel method to autocomplete widget (#226)
Browse files Browse the repository at this point in the history
* added cancel method to autocomplete widget

* removed log
  • Loading branch information
Cameron Mace authored Dec 1, 2016
1 parent d54d576 commit 668bf84
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected void onSaveInstanceState(Bundle outState) {
@Override
protected void onDestroy() {
super.onDestroy();
autocomplete.cancelApiCall();
mapView.onDestroy();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.services.android.geocoder.ui;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,6 +20,7 @@
import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
Expand All @@ -37,6 +39,7 @@ public class GeocoderAdapter extends BaseAdapter implements Filterable {
private double[] bbox;
private Position position;
private int limit;
private Call call;

private GeocoderFilter geocoderFilter;

Expand Down Expand Up @@ -225,6 +228,18 @@ public void setLimit(int limit) {
this.limit = limit;
}

/**
* Can be used to cancel any calls currently in progress. It's a good idea to include in onDestroy() to prevent
* memory leaks
*
* @since 2.0.0
*/
public void cancelApiCall() {
if (call != null) {
call.cancel();
}
}

/*
* Required by BaseAdapter
*/
Expand Down Expand Up @@ -366,6 +381,8 @@ protected FilterResults performFiltering(CharSequence constraint) {
builder.setLimit(limit);
}

call = builder.build().getCall();

// Do request
response = builder.build().executeCall();
} catch (IOException ioException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public boolean onTouch(View view, MotionEvent event) {
});
}

/**
* Can be used to cancel any calls currently in progress. It's a good idea to include in onDestroy() to prevent
* memory leaks
*
* @since 2.0.0
*/
public void cancelApiCall() {
adapter.cancelApiCall();
}

/*
* Setters
*/
Expand Down

0 comments on commit 668bf84

Please sign in to comment.