简体   繁体   English

从WeakReference Java获取参考

[英]Get reference from WeakReference java

Is it same ways to use weekreference? 使用周参考有相同的方法吗?

first way: in the contstructor create WeakReference obj and immediately get my Callbacks obj. 第一种方法:在构造函数中创建WeakReference obj并立即获取我的Callbacks obj。

private static final class LoadImageFromUrl extends AsyncTask<Void, Void, Bitmap> {

    private Callbacks mCallbacks;

    public LoadImageFromUrl(String url, Callbacks callbacks) {
        mCallbacks = new WeakReference<>(callbacks).get();
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        mCallbacks.successfully(bitmap);
    }

second way: in the contstructor create WeakReference obj and get my Callbacks obj when i need something do with it. 第二种方法:在构造函数中创建WeakReference obj并在需要处理时获取我的Callbacks obj。

private static final class LoadImageFromUrl extends AsyncTask<Void, Void, Bitmap> {

    private WeakReference<Callbacks> mCallbacks;

    public LoadImageFromUrl(String url, Callbacks callbacks) {
        mCallbacks = new WeakReference<>(callbacks);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        mCallbacks.get().successfully(bitmap);
    }

I think it similar ways, but not exactly. 我认为这是类似的方式,但不完全相同。

In the first case, it is useless to make a WeakReference because you get the value into it immediately, to store it into a strong reference (the field of your class) 在第一种情况下,创建WeakReference是没有用的,因为您可以立即将其获取值,然后将其存储到强引用中(您的类的字段)

In the second case, it is a real weak reference : if there is no other strong reference of your callback, it could be garbage collected. 在第二种情况下,这是一个真正的弱引用:如果您的回调没有其他强引用,则可能会被垃圾回收。 So mCallbacks.get() could return null in this case. 因此,在这种情况下, mCallbacks.get()可以返回null。

Why do you want to keep your callback s as WeakReference ? 为什么要将callback保留为WeakReference Have you read anything about Strong / Weak / Soft / PhantomReferences ? 您是否阅读过有关“ Strong / Weak / Soft / PhantomReferences

I would advice you to get some knowledge about it and then rethinking your solution. 我建议您先获得一些知识,然后再重新考虑您的解决方案。 Example article 示例文章

It's the second version with the addition of a check whether the callback still exists. 这是第二个版本,其中增加了检查回调是否仍然存在的功能。

private static final class LoadImageFromUrl extends AsyncTask<Void, Void, Bitmap> {

    private WeakReference<Callbacks> mCallbacks;

    public LoadImageFromUrl(String url, Callbacks callbacks) {
        mCallbacks = new WeakReference<>(callbacks);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        Callbacks cb = mCallbacks.get();
        if (cb != null) cb.successfully(bitmap);
    }
}

But that is a legitimate yet dangerous implementation because the caller needs to keep a strong reference. 但这是合法但危险的实现,因为调用者需要保持强有力的引用。 And that not always the case especially with callbacks. 而且并非总是如此,尤其是对于回调。 For example 例如

void onCreate(..) {
    ... 
    new LoadImageFromUrl(new Callbacks() { void foo() .... }).execute();
    ...
}

will fail because nothing keeps a strong reference to the anonymous inner implementation. 将失败,因为没有任何东西可以对匿名内部实现进行严格的引用。 And that's a common way to implement callbacks. 这是实现回调的常用方法。

Semi related: Make sure not to make sure not to extend the LoadImageFromUrl task as inner class because that will cause it to leak the containing class due to the implicit ContainingClass.this reference in every inner class. 半相关:请确保不要确保不将LoadImageFromUrl任务扩展为内部类,因为由于每个内部类中都有隐式的ContainingClass.this引用,这将导致它泄漏包含类。 That would defeat the purpose of weak references. 那会打败弱引用的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM