实例化核心凌空对象 [英] Instantiating core Volley objects

查看:117
本文介绍了实例化核心凌空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么我有点不确定与排球的请求队列,ImageLoader的对象和ImageLoader.ImageCache实现。

What I'm a bit unsure about with Volley is the RequestQueue, ImageLoader objects and ImageLoader.ImageCache implementations..

在我的例子所遇到的,他们被实例化的onCreate(),但它似乎没有意义的创造为每个活动的一个新的请求队列。我也有活动和服务的负载,我会到处使用它。如果我真的要实例化它们在每个服务或活动,多贵他们是谁?

In the examples I have come across they are instantiated in onCreate() but it doesn't seem to make sense to create a new request queue for each and every activity. I also have loads of activities and services and I will be using it everywhere. If I really do have to instantiate them in each Service or Activity, how expensive are they?

什么是最好的做法制作的应用程序使用实例化和访问这些对象?

What is best practice production apps are using to instantiate and access these objects?

推荐答案

我与排球的经验是,我将启动一个请求队列应用程序类传递一个全球范围内的应用程序内。我看不到的缺点这样做只是做一个静态参考请求队列为这样的:

My experience with Volley is that I would initiate a RequestQueue inside of the Application class passing it a global context to the application. I can't see the downside to doing this just make a static reference to the RequestQueue as such:

public class MyApplication extends Application
{
    private static RequestQueue mRequestQueue;

    @Override
    public void onCreate() {
        super.onCreate();
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    // Getter for RequestQueue or just make it public
}

在文档,你可以在应用程序级的IT行情:

In the documentation as you can for the Application class it quotes:

在应用程序启动之前,任何活动,服务,或接收对象(不包括内容提供商)已创建调用。要实现(例如,使用状态的初始化工作)是尽可能快,因为在这个函数所花费的时间会直接影响启动的第一个活动,服务,或接收器在一个进程中的性能。如果覆盖此方法,一定要调用super.onCreate()。

Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created. Implementations should be as quick as possible (for example using lazy initialization of state) since the time spent in this function directly impacts the performance of starting the first activity, service, or receiver in a process. If you override this method, be sure to call super.onCreate().

因此​​,它是安全的假设我们的请求队列将提供给调度请求的服务,活动,装载机等。

So it is safe to assume our RequestQueue will be available to dispatch Requests in a Service, Activity, Loader etc.

现在至于ImageLoader的来讲我会做一个单例类包装一些功能,所以你只有ImageCache的一个实例和一个ImageLoader的,恩。

Now as far as the ImageLoader is concerned I would make a singleton class wrapping some functionality so you only have one instance of ImageCache and one ImageLoader, Ex.

public class ImageLoaderHelper
{
    private static ImageLoaderHelper mInstance = null;

    private final ImageLoader mImageLoader;
    private final ImageCache mImageCache;

    public static ImageLoaderHelper getInstance() {
        if(mInstance == null)
            mInstance = new ImageLoaderHelper();
        return mInstance;
    }

    private ImageLoaderHelper() {
        mImageCache = new MyCustomImageCache();
        mImageLoader = new ImageLoader(MyApplication.getVolleyQueue(),mImageCache);
    }

    // Now you can do what ever you want with your ImageCache and ImageLoader
}

如果你想ImageLoading与凌空一个很好的例子看看这个示例项目是非常有用的。

If you want a really good example of ImageLoading with volley check out this sample project it is really useful.

希望这有助于。

这篇关于实例化核心凌空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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