相比RxBus,EventBus,LiveData有个非常简单的LiveEventBus
参考文章
类似github 项目
如果了解JetPacks原理需要查看我的文章
JetPacks之Lifecycles 原理分析
我们自定义分析下原理
贴一下我们的自定义工具类
public class LiveDataBus {
//存放订阅者
private Map<String, MutableLiveData<Object>> bus;
private static LiveDataBus liveDataBus = new LiveDataBus();
private LiveDataBus() {
bus = new HashMap();
}
public static LiveDataBus getInstance() {
return liveDataBus;
}
//注册订阅者
public synchronized <T> MutableLiveData<T> with(String key, Class<T> type) {
if(!bus.containsKey(key)){
bus.put(key,new MutableLiveData<Object>());
}
return (MutableLiveData<T>)bus.get(key);
}
}
实验
做一个页面跳转,第一个页面传递数据给第二个页面
5s发送一个数据。分别看下LiveDataBus 和LiveDataBusX「接续往下看会实现这个」粘性数据
-> 第一个页面
public void startLiveDataBusActivity() {
//-> LiveDataBus 换成 LiveDataBusX 解决粘性
//-> 粘性数据执行
//-> new LiveData() ——> 绑定Observer —> setValue(onChanged) 正常LiveDataBusX
//-> new LiveData() ——> setValue(onChanged) —> 绑定Observer LiveDataBus
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, TestLiveDataBusActivity.class);
context.startActivity(intent);
new Thread() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
//发送消息
LiveDataBus.getInstance().with("data", String.class).postValue("David-LiveDataBus");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
我们在TestLiveDataBusActivity 这个页面进行接收
public class TestLiveDataBusActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_live_data_bus);
//-> 测试 要吧MainActivity LiveDataBus和LiveDataBusX 保持一致
LiveDataBus.getInstance().with("data",String.class)
.observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
if(s!=null)
Toast.makeText(TestLiveDataBusActivity.this, s, Toast.LENGTH_SHORT).show();
}
});
}
}
我们会发现可能某些场景我们不适合用
我们正常的思维
new LiveData() ——> 绑定Observer —> setValue(onChanged)
先绑定在设置值
而这里忽略我们的顺序变成
new LiveData() ——> setValue(onChanged) —> 绑定Observer -> setValue(onChanged)
和LiveData原理实现有关, 我们可以用反射切断第一次的onChange
解决这个bug,用反射处理
-> 「找Hook 点」
再次我们阅读源码
找到我们的入口
//发送消息
LiveDataBus.getInstance().with("data", String.class).postValue("David-LiveDataBus");
兜了一圈,你会发现,会切换线程,并且给指向setValue方法。
@MainThread
protected void setValue(T value) {
assertMainThread("setValue");
mVersion++;
mData = value;
-> 继续进入查看
dispatchingValue(null);
}
需要注意-----------》 我们dipatchingValue传入的一开始null
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<? super T>, ObserverWrapper>> iterator =
mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {
-> 初始值null参数,所以我们会走for循环
considerNotify(iterator.next().getValue());
if (mDispatchInvalidated) {
break;
}
}
}
} while (mDispatchInvalidated);
mDispatchingValue = false;
}
considerNotify ---------> 这里我们发现了private实现,并且是靠version判断。貌似是Hook的切入点
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);
}
从这个方法我们知道改变mLastVersion 和mVersion的值就可以了。mLastVersion 初始化值 -1。执行过程不一定。但是确实mLastVersion < mVersion 导致继续向下执行
我们需要从这里反向退,最好反向看找到最终的调用点
-> 这个是我们推出来的反射点
SafeIterableMap<Observer<? super T>, ObserverWrapper> mObservers
//ObserverWrapper initiator-> 发现迭代mObservers
void dispatchingValue(@Nullable ObserverWrapper initiator)
-> considerNotify(iterator.next().getValue());
private void considerNotify(ObserverWrapper observer) {
observer.mLastVersion
-> 反射实现代码
public class LiveDataBusX {
//存放订阅者
private Map<String, BusMutableLiveData<Object>> bus;
private static LiveDataBusX liveDataBus = new LiveDataBusX();
private LiveDataBusX() {
bus = new HashMap<>();
}
public static LiveDataBusX getInstance() {
return liveDataBus;
}
//注册订阅者,(存入map) Hook前用MutableLiveData
public synchronized <T> BusMutableLiveData<T> with(String key, Class<T> type) {
if (!bus.containsKey(key)) {
bus.put(key, new BusMutableLiveData<Object>());
}
return (BusMutableLiveData<T>) bus.get(key);
}
public static class BusMutableLiveData<T> extends MutableLiveData<T> {
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
super.observe(owner, observer);
hook(observer);
}
//在这里去改变onChange的流程
private void hook(Observer<? super T> observer) {
try {
//1.得到mLastVersion
//获取到LiveData的类中的mObservers对象
Class<LiveData> liveDataClass = LiveData.class;
Field mObserversField = liveDataClass.getDeclaredField("mObservers");
mObserversField.setAccessible(true);
//获取到这个成员变量的对象
Object mObserversObject = mObserversField.get(this);
//得到map对应的class对象
Class<?> mObserversClass = mObserversObject.getClass();
//获取到mObservers对象的get方法
Method get = mObserversClass.getDeclaredMethod("get", Object.class);
get.setAccessible(true);
//执行get方法
Object invokeEntry = get.invoke(mObserversObject, observer);
//定义一个空的对象
Object observerWraper = null;
if (invokeEntry != null && invokeEntry instanceof Map.Entry) {
observerWraper = ((Map.Entry) invokeEntry).getValue();
}
if (observerWraper == null) {
throw new NullPointerException("observerWraper is null");
}
//得到ObserverWrapper的类对象 编译擦除问题会引起多态冲突所以用getSuperclass
Class<?> superclass = observerWraper.getClass().getSuperclass();
Field mLastVersion = superclass.getDeclaredField("mLastVersion");
mLastVersion.setAccessible(true);
//2.得到mVersion
Field mVersion = liveDataClass.getDeclaredField("mVersion");
mVersion.setAccessible(true);
//3.把mVersion的数据填入到mLastVersion中
Object mVersionValue = mVersion.get(this);
mLastVersion.set(observerWraper, mVersionValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
网友评论