How would an anonymous class get GC'd in picasso on Android?

737 views Asked by At

Can someone explain to me the comment here:

Don't create anonymous class of Target when calling Picasso as might get garbage collected. Keep a member field as a strong reference to prevent it from being gc'ed

Per line 30 of ImageViewAction.java, that Callback is a strong reference.

ImageViewAction(Picasso picasso, ImageView imageView, Request data, boolean skipCache,
      boolean noFade, int errorResId, Drawable errorDrawable, String key, Callback callback) {
    super(picasso, imageView, data, skipCache, noFade, errorResId, errorDrawable, key);
    this.callback = callback;
  }

Assuming the Callback is an anonymous class, it would create a reference to its parent class, thereby preventing the parent from being GC'd too.

Per line 48 of Action.java, the target itself is a WeakReference, but that is not the callback.

  Action(Picasso picasso, T target, Request data, boolean skipCache, boolean noFade,
      int errorResId, Drawable errorDrawable, String key) {
    this.picasso = picasso;
    this.data = data;
    this.target = new RequestWeakReference<T>(this, target, picasso.referenceQueue);

Can someone explain what I am misunderstanding?

1

There are 1 answers

0
esilver On

I was confused and the comment referred to the Target (ImageView) not the Callback. The pattern of using an anonymous Callback is fine.