问题描述
我从排球
使用改造
迁移到一个自定义实现,但我想添加到我实施一些排球
功能,我很喜欢,例如:
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 Thread
s 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!