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();
}
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);
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With