Android开发——LiveData实现原理,sharedpreferences用法
作者:互联网
return mOwner == owner;
}
/* 移除观察 */
@Override
void detachObserver() {
mOwner.getLifecycle().removeObserver(this);
}
}
- ObserverWrapper 这个类里面有个方法,后面的粘性事件会用到,先看看。
void activeStateChanged(boolean newActive) {
//组件状态如果不变返回
if (newActive == mActive) {
return;
}
// immediately set active state, so we’d never dispatch anything to inactive
// owner
mActive = newActive;
changeActiveCounter(mActive ? 1 : -1);
//如果是活动的就发送数据
if (mActive) {
dispatchingValue(this);
}
}
======================================================================
- 这个方法最核心的就是利用主线程Handler发送数据,一步步拆开分析,看看他是怎么写的。
protected void postValue(T value) {
boolean postTask;
//加锁
synchronized (mDataLock) {
postTask = mPendingData == NOT_SET;
//保存要发送的数据value
mPendingData = value;
}
if (!postTask) {
return;
}
//利用主线程Handler发送
ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable);
}
- 在postValue 要发送的 Runnable ,可以看到最后一行就是 setValue 方法,数据也即是之前保存的mPendingData ,只是在这又给 newValue。
private final Runnable mPostValueRunnable = new Runnable() {
@SuppressWarnings(“unchecked”)
@Override
public void run() {
Object newValue;
synchronized (mDataLock) {
newValue = mPendingData;
mPendingData = NOT_SET;
}
setValue((T) newValue);
}
};
4.3 postToMainThread 主线程Handler
- ArchTaskExecutor 类 postToMainThread 方法,实际就是 DefaultTaskExecutor 类执行 postToMainThread 方法。
public class ArchTaskExecutor extends TaskExecutor {
@NonNull
private TaskExecutor mDelegate;
@NonNull
private TaskExecutor mDefaultTaskExecutor;
private ArchTaskExecutor() {
mDefaultTaskExecutor = new DefaultTaskExecutor();
mDelegate = mDefaultTaskExecutor;
}
…
@Override
public void postToMainThread(Runnable runnable) {
mDelegate.postToMainThread(runnable);
}
…
}
-
我们知道是 DefaultTaskExecutor. postToMainThread,就直接看这个方法,哎呀,太熟悉的代码,创建 Handler ,传入的是 Looper.getMainLooper() ,就是主线程Handler ,然后就 post 消息。
-
如果不了解 Handler ,可以看 Android Handler 从使用到进阶。
public class DefaultTaskExecutor extends TaskExecutor {
@Nullable
private volatile Handler mMainHandler;
@Override
public void postToMainThread(Runnable runnable) {
if (mMainHandler == null) {
synchronized (mLock) {
if (mMainHandler == null) {
mMainHandler = createAsync(Looper.getMainLooper());
}
}
}
//noinspection ConstantConditions
mMainHandler.post(runnable);
}
}
-
mVersion 在初始化的构造方法里就赋值了,为-1,每次setValue,版本号就会变一次。
-
setValue 也就是用 mData 保存一下 value ,然后交给 dispatchingValue 方法处理。
@MainThread
protected void setValue(T value) {
assertMainThread(“setValue”);
mVersion++;
mData = value;
dispatchingValue(null);
}
- setValue走该方法,传的 initiator为空,那就遍历 mObservers 保存的观察者发送数据。
@SuppressWarnings(“WeakerAccess”) /* synthetic access */
void dispatchingValue(@Nullable ObserverWrapper initiator) {
if (mDispatchingValue) {
mDispatchInvalidated = true;
return;
}
mDispatchingValue = true;
do {
mDispatchInvalidated = false;
if (initiator != null) {
//粘性事件,就单个观察者接受数据
considerNotify(initiator);
initiator = null;
} else {
//setvalue 传过来 initiator 为空,执行这里
// mObservers 拿出来,逐个发送数据
for (Iterator<Map.Entry<Observer<? super T>, ObserverWrapper>> iterator =
mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {
considerNotify(iterator.next().getValue());
if (mDispatchInvalidated) {
break;
}
}
}
} while (mDispatchInvalidated);
mDispatchingValue = false;
}
-------------------------------------------------------
-
这里先判断组件是否活动。
-
在判断粘性事件。
-
然后用 mVersion ,判断是否发送过数据。
-
最后才是给观察者发送数据。
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;
//发送数据
observer.mObserver.onChanged((T) mData);
}
r 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;
//发送数据
observer.mObserver.onChanged((T) mData);
}
标签:setValue,return,observer,void,LiveData,Handler,sharedpreferences,发送数据,Android 来源: https://blog.csdn.net/sa3221sa/article/details/122581184