【发布时间】:2013-12-10 21:21:03
【问题描述】:
谁能给我解释一下comment here:
在调用 Picasso 时不要创建匿名的 Target 类 收集垃圾。保留成员字段作为强引用 防止它被 gc'ed
根据line 30 of ImageViewAction.java,回调是一个强引用。
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;
}
假设 Callback 是一个匿名类,它将创建对其父类的引用,从而防止父类也被 GC。
根据line 48 of Action.java,目标本身是一个弱引用,但这不是回调。
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);
谁能解释我的误解?
【问题讨论】:
-
某人必须持有对您的
Target的引用,否则它将被 gc'ed 因为毕加索只存储了一个WeakReference。 -
啊,我明白了 - 您的评论仅适用于目标而不是回调。那么使用匿名回调应该没问题吧?
-
是的,匿名回调被保留为强参考。强烈建议您调用
cancel(target)以释放引用并防止在 Picasso 下载完成之前临时泄漏。
标签: android anonymous-inner-class picasso