JetPackMVVM架构

LiveDataBus

2019-09-25  本文已影响0人  leap_

LiveDataBus是基于LiveData实现的类似EventBus的消息通信框架,它是基于LiveData实现的,完全可以代替EventBus,RxBus;

往期通信框架的缺点:
LiveDataBus的优点:

发布订阅模式

LiveDataBus的主要是基于发布订阅设计模式,发布订阅模式定义了一种 “一对多” 的关系,和观察者模式是完全不同的两个设计模式;

举个例子

LiveData

下面详细介绍核心类LiveData

LiveData是一个可以被观察的数据holder,并且可以自动感知控件的生命周期,不会发生内存泄漏;

LiveData需要一个观察者对象,当LiveData的值发生改变时,观察者会察觉到这个改变;

liveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {

            }
        });

使用livedata注册观察者监听

mutableLiveData.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                
            }
        });

使用livedata发送消息给观察者

 mutableLiveData.postValue("发送的消息");

LiveData其实就是一个存放数据的holder,类似ViewHolder的holder,存放在LiveData里的数据会拥有LiveData的特性;

LiveData的特性:

LiveData是Android Architecture Components的一个类;这个类是谷歌在Google I/O 2017发布一套帮助开发者解决Android架构设计的方案。这个类有四个核心,后续会一一介绍;

LifeDataBus的原理


LifeDataBus的组成:基于发布订阅模式

LiveDataBus的第一代实现

public class LiveDataBus {
    
    // 消息总线
    private Map<String,MutableLiveData<Object>> bus;

    // 单例模式(静态内部类法)
    private LiveDataBus(){
        bus = new HashMap<>();
    }
    private static class SingleInstance{
       private static LiveDataBus mInstance = new LiveDataBus();
    }
    public static LiveDataBus get(){
        return SingleInstance.mInstance;
    }

    // 获取消息通道
    public<T> MutableLiveData<T> getChanel(String target,Class<T> type){
        if (!bus.containsKey(target)){
            bus.put(target, new MediatorLiveData<>());
        }
        return (MutableLiveData<T>) bus.get(target);
    }
    public MutableLiveData<Object> getChanel(String target){
        return getChanel(target,Object.class);
    }
    
}
我们使用单例确保只有一个消息总线(Map),在消息总线中存放消息通道(LiveData),通过map的key去匹配消息通道两端的发送者和订阅者。

用第一代LiveDataBus订阅

// 注册LiveDataBus
        LiveDataBus.get().getChanel("yingyingying").observe(this, new android.arch.lifecycle.Observer<Object>() {
            @Override
            public void onChanged(@Nullable Object o) {
                Toast.makeText(getApplicationContext(),""+o.toString(),Toast.LENGTH_LONG).show();
            }
        });

发送消息

tv_hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiveDataBus.get().getChanel("yingyingying",String.class).postValue("嘤嘤嘤");
            }
        });
接收到消息

至于说他是第一代bus,说明肯定有问题,问题就是在post或者set一个value后,只要在一个frag/act里observe了,无论组件是否启动,都会收到value,即当在act1中post了,在act2中observe,但是post的时候act2没有运行,当启动act2,收到了value。收到了订阅前的消息

分析原因——LiveData的原理

通过查看LiveData的源码发现setValue()开始,依次调用了
dispatchValue() -> considerNotify() -> observer.onChanged()

protected void setValue(T value) {
        assertMainThread("setValue");
        mVersion++;
        mData = value;
        dispatchingValue(null);
    }
private void dispatchingValue(@Nullable ObserverWrapper initiator) {
        if (mDispatchingValue) {
            mDispatchInvalidated = true;
            return;
        }
        mDispatchingValue = true;
        do {
            mDispatchInvalidated = false;
            if (initiator != null) {
                considerNotify(initiator);
                initiator = null;
            } else {
                for (Iterator<Map.Entry<Observer<T>, ObserverWrapper>> iterator =
                        mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {
                    considerNotify(iterator.next().getValue());
                    if (mDispatchInvalidated) {
                        break;
                    }
                }
            }
        } while (mDispatchInvalidated);
        mDispatchingValue = false;
    }
private void considerNotify(ObserverWrapper observer) {
        if (!observer.mActive) {
            return;
        }
        // Check latest state b4 dispatch. Maybe it changed state but we didn't get the event yet.
        //
        // we still first check observer.active to keep it as the entrance for events. So even if
        // the observer moved to an active state, if we've not received that event, we better not
        // notify for a more predictable notification order.
        if (!observer.shouldBeActive()) {
            observer.activeStateChanged(false);
            return;
        }
        if (observer.mLastVersion >= mVersion) {
            return;
        }
        observer.mLastVersion = mVersion;
        //noinspection unchecked
        observer.mObserver.onChanged((T) mData);
    }

postValue()会调用setValue()所以同理;
这就解释了为什么我们可以在observer中收到post来的value,为什么act2不运行也可以收到value;
我们注意到当observer.mLastVersion >= mVersion的时候会直接return,不调用onChanged从而解决上面的问题。

根据上面的分析,我们只需要在订阅的时候,将observe的版本等于livedata的版本即可,这样livedata就不会强制调用onChanged();

我们需要拿到mLastVersion,就需要拿到observer对象,顺着源码发现observer对象存在mObservers的map中;我们自定义一个mutableLiveData,改写他的observe(),在observe()中,通过反射拿到mObservers对象,从而拿到observer.mLastVersion,将mVersion赋值给他;

LiveDataBus2.0

public class LiveDataBus2 {

    private final Map<String, BusMutableLiveData<Object>> bus;

    private LiveDataBus2() {
        bus = new HashMap<>();
    }

    private static class SingletonHolder {
        private static final LiveDataBus2 DEFAULT_BUS = new LiveDataBus2();
    }

    public static LiveDataBus2 get() {
        return SingletonHolder.DEFAULT_BUS;
    }

    public <T> MutableLiveData<T> with(String key, Class<T> type) {
        if (!bus.containsKey(key)) {
            bus.put(key, new BusMutableLiveData<>());
        }
        return (MutableLiveData<T>) bus.get(key);
    }

    public MutableLiveData<Object> with(String key) {
        return with(key, Object.class);
    }

    private static class ObserverWrapper<T> implements Observer<T> {

        private Observer<T> observer;

        public ObserverWrapper(Observer<T> observer) {
            this.observer = observer;
        }

        @Override
        public void onChanged(@Nullable T t) {
            if (observer != null) {
                if (isCallOnObserve()) {
                    return;
                }
                observer.onChanged(t);
            }
        }

        private boolean isCallOnObserve() {
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            if (stackTrace != null && stackTrace.length > 0) {
                for (StackTraceElement element : stackTrace) {
                    if ("android.arch.lifecycle.LiveData".equals(element.getClassName()) &&
                            "observeForever".equals(element.getMethodName())) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    private static class BusMutableLiveData<T> extends MutableLiveData<T> {

        private Map<Observer, Observer> observerMap = new HashMap<>();

        @Override
        public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
            super.observe(owner, observer);
            try {
                // 设置observer的version和LiveData一致
                hook(observer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void observeForever(@NonNull Observer<T> observer) {
            if (!observerMap.containsKey(observer)) {
                observerMap.put(observer, new ObserverWrapper(observer));
            }
            super.observeForever(observerMap.get(observer));
        }

        @Override
        public void removeObserver(@NonNull Observer<T> observer) {
            Observer realObserver = null;
            if (observerMap.containsKey(observer)) {
                realObserver = observerMap.remove(observer);
            } else {
                realObserver = observer;
            }
            super.removeObserver(realObserver);
        }

        private void hook(@NonNull Observer<T> observer) throws Exception {
            // 获取livedata的class对象
            Class<LiveData> classLiveData = LiveData.class;
            // 获取   LiveData类的mObservers对象 (Map对象)的 Field对象
            Field fieldObservers = classLiveData.getDeclaredField("mObservers");
            // 将mObservers 的private设置为 public
            fieldObservers.setAccessible(true);
            //  获取当前livedata的mObservers对象(map)
            Object objectObservers = fieldObservers.get(this);
            // 拿到mObservers(map)的class对象
            Class<?> classObservers = objectObservers.getClass();
            // 通过map的class对象拿到get()的method对象
            Method methodGet = classObservers.getDeclaredMethod("get", Object.class);
            methodGet.setAccessible(true);
            // 通过map 的 get Method对象 拿到值 (Entry)  (arg1:map ,arg2:key )
            Object objectWrapperEntry = methodGet.invoke(objectObservers, observer);
            // 拿到wrapper
            Object objectWrapper = null;
            if (objectWrapperEntry instanceof Map.Entry) {
                objectWrapper = ((Map.Entry) objectWrapperEntry).getValue();
            }
            if (objectWrapper == null) {
                throw new NullPointerException("Wrapper can not be bull!");
            }
            // 反射wrapper对象
            Class<?> classObserverWrapper = objectWrapper.getClass().getSuperclass();
            // 拿到wrapper的version
            Field fieldLastVersion = classObserverWrapper.getDeclaredField("mLastVersion");
            fieldLastVersion.setAccessible(true);
            //get livedata's version
            Field fieldVersion = classLiveData.getDeclaredField("mVersion");
            fieldVersion.setAccessible(true);
            Object objectVersion = fieldVersion.get(this);
            //set wrapper's version
            fieldLastVersion.set(objectWrapper, objectVersion);
        }
    }
}

hook的作用相当于 在observe()调用后执行observer.mLastVersion = mVersion; 让considerNotify()直接return,可是我们如何收到订阅后的post呢?因为只有订阅的时候才会hook,在hook后,我们调用post(),会mVersion++,所以在判断 if (observer.mLastVersion >= mVersion) 的时候就又会是false了;

上一篇下一篇

猜你喜欢

热点阅读