Reputation: 56
Is there any way to find if one request is still executing in retrofit or not? I want to cancel request if one request is still executing and isn't completed yet.
// fetch Results
private void fetchResults(CharSequence s) {
if (s.toString().length() > 3){
mSearchResult.clear();
mAdapter.notifyDataSetChanged();
HashMap hashMap = new HashMap();
hashMap.put("q", s.toString());
PrefManager pref = new PrefManager(SearchActivity.this);
String token = pref.getAuthenticationToken();
Call<AppointmentsModel> call = mAPIService.getResults("Token " + token, hashMap);
call.enqueue(new Callback<AppointmentsModel>() {
@Override
public void onResponse(Call<AppointmentsModel> call, Response<AppointmentsModel> response) {
List<Result> resultList = response.body().getResult();
mSearchResult.addAll(resultList);
mAdapter.notifyDataSetChanged();
showNoSearchResults();
}
@Override
public void onFailure(Call<AppointmentsModel> call, Throwable t) {
if (BuildConfig.DEBUG) Log.d(TAG, "onFailureRetrofit: " + t.toString());
}
});
}else{
showNoSearchResults();
}
}
I want to cancel call whenever one request is already working. Can you tell me where should i do the same. Thanks.
Upvotes: 1
Views: 5119
Reputation: 820
Make it as global:
Call<AppointmentsModel> call;
public void afterTextChanged (Editable s) {
if(call != null && call.isExecuted()) {
call.cancel();
}else{
//Your network call
}
}
Upvotes: 1