Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to create new Instance of picasso everytime

Is it okay to create a new instace of picasso for loading every image.For E.g something like:

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.error)
    .centerInside(
    .tag(context)
    .into(holder.image);

in getView() of a listAdaptor.Does it not create new LruCache everytime which will eventually lead to OOM.

Also can the Context passed to Picasso can be an Activity Context:

/** Start building a new {@link Picasso} instance. */
public Builder(Context context) {
  if (context == null) {
    throw new IllegalArgumentException("Context must not be null.");
  }
  this.context = context.getApplicationContext();
}
like image 537
Shubham Avatar asked Jan 08 '15 10:01

Shubham


3 Answers

Picasso is designed to be a singleton, so there's isn't a new instance created every time.

This is the with() method :

public static Picasso with(Context context) {
    if (singleton == null) {
      synchronized (Picasso.class) {
        if (singleton == null) {
          singleton = new Builder(context).build();
        }
      }
    }
    return singleton;
}

Note that it is a static method so you don't call with() on a particular instance, Picasso is managing its own instance, which is only created if singleton is null.

There's no problem with passing an Activity as the context, because the Builder will use the ApplicationContext which is a single, global Application object of the current process :

public Builder(Context context) {
      if (context == null) {
        throw new IllegalArgumentException("Context must not be null.");
      }
      this.context = context.getApplicationContext();
}

As for the cache, the same one is use everytime, since it is retained by the singleton :

public Picasso build() {
      // code removed for clarity

      if (cache == null) {
        cache = new LruCache(context);
      }
      // code removed for clarity

      return new Picasso(context, dispatcher, cache, listener, transformer, requestHandlers, stats,
          defaultBitmapConfig, indicatorsEnabled, loggingEnabled);
}
like image 123
2Dee Avatar answered Sep 16 '22 20:09

2Dee


Kalyan is right! Picasso is already a singleton, so it uses the same instance for all the images that you load. Also that means that you will not need that builder. you simply just call: "Picasso.with(context).load(url).into(holder.image);" with the settings you want and that is all.

like image 39
rosu alin Avatar answered Sep 20 '22 20:09

rosu alin


Its not problem..You are not creating Object 

Picasso is already SingleTon Object

Here is the Souce code Picasso Class

public static Picasso with(Context context) {
    if (singleton == null) {
        synchronized (Picasso.class) {
            if (singleton == null) {
                singleton = new Builder(context).build();
            }
        }
    }
    return singleton;
}

More Source code at Picasso Source code

like image 45
kalyan pvs Avatar answered Sep 20 '22 20:09

kalyan pvs