Retrofit v2 Does Call.cancel () remove callback?

I read in Retrofit v2 API Spec that calling the cancel () method in the Call class returned from your user interface for re-equipment should set the callback to null.

cancel () is no-op after receiving a response. In all other cases, the method will set any callbacks to null (thus freeing strong references to the enclosing class, if anonymously declared)

Looking through the code, I do not see that Callback is explicitly set to null when the cancel call. I see that the callback refers to the OkHttpCall class (although it is not explicitly saved). Cancellation cancellation in turn cancels the call in the RealCall class, which takes care of the Http side to cancel, but does not apply to the stored callback in the AsyncCall class (which is placed in the readyAsyncCalls and runningAsyncCalls queues in the Dispatcher class. For me, the code is unfamiliar, so I can that skip something.

Can someone confidently confirm that a cancel () call on my call object will delete the callback link that I passed, so I'm not a memory leak?

Simplified code example:

@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); api = retrofit.create(Api.class); } @Override public void onStart() { super.onStart(); call = api.getPokedex(); call.enqueue(new Callback<Pokedex>() { @Override public void onResponse(Call<Pokedex> call, Response<Pokedex> response) { populate(response.body()); } @Override public void onFailure(Call<Pokedex> call, Throwable t) { error(t.getMessage()); } }); } @Override public void onStop() { call.cancel(); super.onStop(); } 
+6
source share

All Articles