简体   繁体   English

Android数据绑定库是否在其回调中包含强引用?

[英]Does the Android Data Binding Library hold Strong References on It's Callbacks?

I recently read somewhere that the Data Binding Library would store it's listeners as WeakReferences. 我最近在某处读到,数据绑定库会将其侦听器存储为WeakReferences。 Well I couldn't believe it so I took a look at the ObservableArrayMap implementation in the Data Binding Library: 好吧,我简直不敢相信,所以我看了一下数据绑定库中的ObservableArrayMap实现:

private transient MapChangeRegistry mListeners;

...

@Override
public void addOnMapChangedCallback(
        OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> listener) {
    if (mListeners == null) {
        mListeners = new MapChangeRegistry();
    }
    mListeners.add(listener);
}

The code snipped above points me to the MapChangeRegistry which extends CallbackRegistry : 上面插入的代码将我指向扩展CallbackRegistryMapChangeRegistry

private List<C> mCallbacks = new ArrayList<C>();

...

public synchronized void add(C callback) {
    if (callback == null) {
        throw new IllegalArgumentException("callback cannot be null");
    }
    int index = mCallbacks.lastIndexOf(callback);
    if (index < 0 || isRemoved(index)) {
        mCallbacks.add(callback);
    }
}

There is a lot going on around, but as I understood the callback is stored inside a List, which tells me if I don't unregister my Callbacks with eg. 有很多事情发生,但是据我了解,回调函数存储在一个List中,它告诉我是否不使用例如注销回调。 removeOnMapChangedCallback() I'd risk memory leaks. removeOnMapChangedCallback()我冒着内存泄漏的风险。

My java memory management skills are limited to trust the GC and carefully use WeakReferences (I'd like to improve on that topic someday, when I have the time). 我的Java内存管理技能仅限于信任GC并谨慎使用WeakReferences(我希望有空的时候有一天可以在该主题上进行改进)。 But for now I'd like to know if I interpreted my reasearch correctly and that I'd also have to use typical Observer register/unregister boilerplate code when using the Data Binding Library. 但是现在,我想知道我是否正确解释了我的reasearch,并且在使用数据绑定库时还必须使用典型的Observer注册/取消注册样板代码。

The observed items aren't responsible for keeping weak references to the things being notified. 观察到的项目不负责保持对所通知事物的弱引用。 Because Observable is an interface, the data binding system can't trust that the implementation uses weak references. 因为Observable是一个接口,所以数据绑定系统不能相信该实现使用弱引用。

Instead, the observed items are tied back to the generated binding using an intermediate weak reference class to the binding class. 而是使用绑定类的中间弱引用类将观察到的项绑定到生成的绑定。 It is somewhat complex because of there are so many different types of things to observe, but the implementation is in ViewDataBinding.java and you can see how it is implemented. 由于要观察的东西种类繁多,所以它有些复杂,但是该实现在ViewDataBinding.java中,您可以看到它是如何实现的。

Essentially the observing is: 本质上,观察是:

Observed Object ===> Observer - - -> Binding class 观察对象===>观察者--->绑定类

where the - - - is the weak reference and === is a strong reference. 其中---是弱参考,而===是强参考。 Because the Observer is a weak reference, it doesn't generate object count overhead that would happen if it had a weak reference. 因为观察者一个弱引用,所以它不会产生对象引用开销,如果它具有弱引用会发生这种情况。

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

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