Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform requests with Retrofit inside custom Runnable

I am migrating from Volley to a custom implementation using Retrofit, but I'm trying to add to my implementation some of the Volley features that I liked, for example

RequestQueue.cancel(String tag)

If the Request has the requested tag, then it's canceled by setting a boolean value, mCanceled, to true. The run method checks this value and returns if it's true. To be able to reproduce this with Retrofit I should be able to use my custom class implementing Runnable instead of the default one, where I have a mTag and a mCanceled field. Moreover, Volley was also able to set such flag inside the active Threads and immediately stop them. My cancelAll method, that I've already implemented, just drains the queue to another queue, but isn't able to access the active threads. Is it possible to achieve the same results with Retrofit and ThreadPoolExecutor?

like image 734
Vektor88 Avatar asked Apr 24 '14 13:04

Vektor88


2 Answers

I think I've found a nicer solution: instead of blocking the Runnable of the requests, I am blocking the Callback execution.

I have extended the Callback interface:

public interface CustomCallbackInterface<T> extends Callback<T> {
    public String getTag();
    public String setTag(String tag);
    public void cancel();
    public boolean isCanceled();
}

so that each Callback has a tag and a cancel flag. Then the success method starts with:

public class CustomCallback<ConvertedData> implements CustomCallbackInterface<ConvertedData>{

    //failure...

    @Override
    public void success(ConvertedData cd, Response response) {
        if(isCanceled()) return;
        // ....
    }
}

Every time I make a new request, I store the created CustomCallback inside a List cancel just iterates the list and calls cancel() on the items with the same tag.

like image 105
Vektor88 Avatar answered Oct 22 '22 08:10

Vektor88


I've implemented an easy to use class based on Vektor88 answer

public abstract class CancelableCallback<T> implements Callback<T> {

    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(T 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(T t, Response response);

    public abstract void onFailure(RetrofitError error);
}

Usage example

rest.request(..., new CancelableCallback<MyResponse>(TAG) {
    @Override
    public void onSuccess(MyResponse myResponse, Response response) {
        ...
    }

    @Override
    public void onFailure(RetrofitError error) {
       ...
    }
});

// if u need to cancel all
CancelableCallback.cancelAll();
// or cancel by tag
CancelableCallback.cancel(TAG);
like image 41
Biggemot Avatar answered Oct 22 '22 10:10

Biggemot