Android Volley Singleton Pattern如何基于标签添加/取消请求 [英] Android Volley Singleton Pattern how to add/cancel requests based on tag

查看:87
本文介绍了Android Volley Singleton Pattern如何基于标签添加/取消请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 https://developer.android.com/training/volley/requestqueue.html#singleton 不鼓励通过在Application.onCreate()

According to https://developer.android.com/training/volley/requestqueue.html#singleton it is discouraged to use the old way of implementing a singleton class by setting up the RequestQueue in Application.onCreate()

所提供的新的"模块化方式如下所示,但是不包含将标签添加到请求并使用这些标签取消它们的方法.

The provided "new" more modular way as seen below however doesn't contain a method for adding tags to requests and cancelling them using these tags.

public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;

private MySingleton(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap>
                cache = new LruCache<String, Bitmap>(20);

        @Override
        public Bitmap getBitmap(String url) {
            return cache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            cache.put(url, bitmap);
        }
    });
}

public static synchronized MySingleton getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new MySingleton(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}

}

我该如何添加与旧方法(在Application.onCreate()中)类似的方法:

How do I go about adding the methods similar to the ones below from the old way (in Application.onCreate()):

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}

推荐答案

关于RequestQueue的任何方面都没有改变.您只是从与Application类不同的单例访问它.始终以相同的方式将标签添加到请求中:

Nothing about RequestQueue has changed in that respect. You're just accessing it from a different singleton than the Application class. Add the tags to the requests the same way you always would:

ImageRequest request = new ImageRequest(...);
request.setTag(MY_TAG);
MySingleton.getInstance(this).addToRequestQueue(request);

-编辑-

进一步阐述:这与从其他任何地方获取RequestQueue对象相同.它仍然只是一个RequestQueue.

Further elaboration: it's the same as if you were to get the RequestQueue object from anywhere else. It's still just a RequestQueue.

所以这个:

mRequestQueue.cancelAll(tag);

成为:

MySingleton.getRequestQueue().cancelAll(tag);

您之前使用RequestQueue所做的所有其他操作也是如此.示例MySingleton类只是为您保留了请求队列.它没有改变.

The same goes for anything else you were previously doing with a RequestQueue. The example MySingleton class there is just acting to hold on to the request queue for you. It isn't changing it.

要取消所有内容,无论使用什么标签:

To cancel absolutely everything, no matter what tag:

MySingleton.getRequestQueue().cancelAll(new RequestQueue.RequestFilter() {
    @Override
    public boolean apply(Request<?> request) {
        return true;
    }
});

这篇关于Android Volley Singleton Pattern如何基于标签添加/取消请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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