执行与改造的要求内定制了Runnable [英] Perform requests with Retrofit inside custom Runnable

查看:132
本文介绍了执行与改造的要求内定制了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(字符串标签)

如果在要求有要求的标记,那么它取消了通过设置布尔值, mCanceled ,为true。该运行方法检查该值,如果它是真实的回报。 为了能够重现这与改造我应该可以使用的Runnable 而不是默认的我的自定义类实现,在那里我有一个 MTAG mCanceled 字段。 此外,排球还可以设置这样的标志活动的内线,并立即阻止他们。我的 cancelAll 的方法,我已经实现了,只是水渠排队到另一个队列,但无法访问活动线程。 是否有可能实现与改造的ThreadPoolExecutor

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?

推荐答案

我想我已经找到一个更好的解决办法:不是挡住了的Runnable 的要求,我我挡住了回调执行。

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

我已经扩展了回调接口:

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;
        // ....
    }
}

每当我提出一个新的要求,我将创建的 CustomCallback 列表取消只是迭代名单并要求取消()在同一个标​​签的物品。

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.

这篇关于执行与改造的要求内定制了Runnable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆