在 GenericLifecycleObserver.onStateChanged(LifecycleOwner, Lifecycle.Event) 中能接受到回调

LiveData.java
------------------------------
public abstract class LiveData {

class LifecycleBoundObserver extends ObserverWrapper implements GenericLifecycleObserver {
@NonNull
final LifecycleOwner mOwner;

@Override
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {
removeObserver(mObserver);
return;
}
activeStateChanged(shouldBeActive());
}
}


@MainThread
public void removeObserver(@NonNull final Observer observer) {
assertMainThread("removeObserver");
ObserverWrapper removed = mObservers.remove(observer);
if (removed == null) {
return;
}
removed.detachObserver();
removed.activeStateChanged(false);
}



LiveEventBus.java
------------------------------
public final class LiveEventBus {

private class LiveEvent implements Observable {

private class LifecycleLiveData extends ExternalLiveData {
@Override
protected Lifecycle.State observerActiveLevel() {
return lifecycleObserverAlwaysActive ? Lifecycle.State.CREATED : Lifecycle.State.STARTED;
}

@Override
public void removeObserver(@NonNull Observer observer) {
super.removeObserver(observer);
if (autoClear && !liveData.hasObservers()) {
LiveEventBus.get().bus.remove(key);
}
}
}
前面讲过在 LiveData 的内部类?LifecycleBoundObserver.onStateChanged 方法中会判断如果当前 state 是 DESTROYED,就删除观察者操作。这个要删除的观察者 Observer 是 LiveData 的所包裹的数据的观察者。

具体在 LiveData 代码的逻辑是,把这个observer 从 mObservers集合(LiveData 所缓存的一份观察者列表)?中移除。

?

 

对应到我们 LiveEventBus ,就是我们使用 LiveEventBus 传进去的 Observer 回调函数给移除了。

这样就实现了「自动取消订阅」的效果。


?

还有LiveEventBus声称具有「整个生命周期(从onCreate到onDestroy)都可以实时收到消息」能力,那么怎么实现的呢?

ps:这算值得写出来的能力吗?这不是一个正常的事件总线应有的能力吗?

ExternalLiveData.java
------------------------------
public class ExternalLiveData extends MutableLiveData {

protected Lifecycle.State observerActiveLevel() {
return CREATED;
}

class ExternalLifecycleBoundObserver extends LifecycleBoundObserver {

ExternalLifecycleBoundObserver(@NonNull LifecycleOwner owner, Observer observer) {
super(owner, observer);
}

@Override
boolean shouldBeActive() {
return mOwner.getLifecycle().getCurrentState().isAtLeast(observerActiveLevel());
}
}



LiveEventBus.java
------------------------------
public final class LiveEventBus {

private boolean lifecycleObserverAlwaysActive = true;

private class LifecycleLiveData extends ExternalLiveData {
@Override
protected Lifecycle.State observerActiveLevel() {
return lifecycleObserverAlwaysActive ? Lifecycle.State.CREATED : Lifecycle.State.STARTED;
}



LiveData.java
------------------------------
public abstract class LiveData {


class LifecycleBoundObserver extends ObserverWrapper implements GenericLifecycleObserver {

@Override
boolean shouldBeActive() {
return mOwner.getLifecycle().getCurrentState().isAtLeast(STARTED);
}
原生 LiveData.java 使用了?LifecycleBoundObserver,所以在判定是否活跃的这个 shouldBeActive() 方法中,owner 的生命周期 state ≥?STARTED 才会认为是活跃。即?STARTED 和?RESUMED。符合 MVVM 的特性,可见的界面才有必要更新UI。

LiveEventBus 如果使用这个策略是不太合理的,所以重写了?shouldBeActive() 方法,让它判断逻辑是≥CREATED(LiveEventBus的默认情况)即认为是活跃。达到了官网宣称的「整个生命周期(从onCreate到onDestroy)都可以实时收到消息」效果。
————————————————

转载于:https://www.cnblogs.com/hyhy904/p/11384645.html

你可能感兴趣的:(在 GenericLifecycleObserver.onStateChanged(LifecycleOwner, Lifecycle.Event) 中能接受到回调)