相比RxBus,EventBus,LiveData有个非常简单的LiveEventBus
参考文章
类似github 项目
如果了解JetPacks原理需要查看我的文章
JetPacks之Lifecycles 原理分析
我们自定义分析下原理
贴一下我们的自定义工具类
public class LiveDataBus {
//存放订阅者
private Map> bus;
private static LiveDataBus liveDataBus = new LiveDataBus();
private LiveDataBus() {
bus = new HashMap();
}
public static LiveDataBus getInstance() {
return liveDataBus;
}
//注册订阅者
public synchronized MutableLiveData with(String key, Class type) {
if(!bus.containsKey(key)){
bus.put(key,new MutableLiveData
实验
做一个页面跳转,第一个页面传递数据给第二个页面
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() {
@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, 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, 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> bus;
private static LiveDataBusX liveDataBus = new LiveDataBusX();
private LiveDataBusX() {
bus = new HashMap<>();
}
public static LiveDataBusX getInstance() {
return liveDataBus;
}
//注册订阅者,(存入map) Hook前用MutableLiveData
public synchronized BusMutableLiveData with(String key, Class type) {
if (!bus.containsKey(key)) {
bus.put(key, new BusMutableLiveData