Faced a decision that prompted an unexpected question for me. In his application (using retrofit'a 1.9) implemented the loading of a photo (from the gallery). From which the next idea emerged that the user can at any time simply cancel the download of this photo. Therefore, it was necessary to implement Cancel Request , given that in version 1.9 it is very painful . But again, the crux of the matter is not in this, it was decided to do something as follows, namely, to implement the CancelableCallback interface:

public abstract class CancelableCallback implements Callback<ImageUpload> { private static List<CancelableCallback> mList = new ArrayList<>(); private boolean isCanceled = false; private Object mTag = null; public static void cancelAll() { Iterator<CancelableCallback> iterator = mList.iterator(); while (iterator.hasNext()) { iterator.next().isCanceled = true; iterator.remove(); } } public static void cancel(Object tag) { if (tag != null) { Iterator<CancelableCallback> iterator = mList.iterator(); CancelableCallback item; while (iterator.hasNext()) { item = iterator.next(); if (tag.equals(item.mTag)) { item.isCanceled = true; iterator.remove(); } } } } public CancelableCallback() { mList.add(this); } public CancelableCallback(Object tag) { mTag = tag; mList.add(this); } public void cancel() { isCanceled = true; mList.remove(this); } @Override public final void success(ImageUpload t, Response response) { if (!isCanceled) onSuccess(t, response); mList.remove(this); } @Override public final void failure(RetrofitError error) { if (!isCanceled) onFailure(error); mList.remove(this); } public abstract void onSuccess(ImageUpload t, Response response); public abstract void onFailure(RetrofitError error); } 

After that, implement in our fragment:

 CancelableCallback.cancel(TAG); 

And actually the question is, TAG how to handle it correctly and what are the ways to use it. So far I know that Tag is a kind of Object-storage for each View, where we can put the data you need ( I would like more detail ).

  • tag used to store data or to search through it in the hierarchy - Alexey Malchenko
  • @ Alexey Malchenko okay, correct me, if not right. Here in the code I have CancelableCallback.cancel (TAG); Should I import my data there or how? I do not understand. + How is it possible to use this command by clicking on an element?
  • one
    You need to set up ImageView and Callback. Get a Map <Callback, ImageView> associate the request with the ImageView there and after completing the Callback, pull out and update the ImageView you need. To cancel, simply delete the link. Also note that on one ImageView there is a chance to create several requests, this can be bad for you. Do not forget to clean the connection after the removal of the Activity, or you may fall. Or just use the Picasso library. - Alexey Malchenko
  • @ Alexey Malchenko The project uses this library, but by simply you meant probably an easier way? Alas, this library has not worked yet. - Morozov

0