Is it possible to create a new picasso instance every time?

Is it possible to create a new picasso file to load each image. For example, for example:

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

in getView() for listAdaptor . It does not create a new LruCache every time, which ultimately leads to OOM.

You can also pass Context to Picasso as 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(); } 
+5
source share
3 answers

Picasso is designed as a singleton, so every new instance is not 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 this is a static method, so you do not call with() on a specific instance, Picasso manages its own instance, which is only created if singleton is null .

There is no problem passing Activity as a 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 used every time, since it is stored in 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); } 
+11
source
 Its not problem..You are not creating Object 

Picasso already a SingleTon Object

Here is the Picasso class Souce code

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

More Details Source Code Picasso Source Code

+1
source

Kalyan is right! Picasso is already singleton, so it uses the same instance for all uploaded images. It also means that you do not need this builder. you just call: "Picasso.with (context) .load (URL) .into (holder.image);" with the settings you want and that's it.

+1
source

Source: https://habr.com/ru/post/1210649/


All Articles