其他分享
首页 > 其他分享> > Android开发——LiveData实现原理,sharedpreferences用法

Android开发——LiveData实现原理,sharedpreferences用法

作者:互联网

return mOwner == owner;

}

/* 移除观察 */

@Override

void detachObserver() {

mOwner.getLifecycle().removeObserver(this);

}

}

3.3 activeStateChanged 方法


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);

}

}

3.4 简易流程图


LiveData注册

4.发送数据分析

======================================================================

4.1 postValue 发送数据到主线程


protected void postValue(T value) {

boolean postTask;

//加锁

synchronized (mDataLock) {

postTask = mPendingData == NOT_SET;

//保存要发送的数据value

mPendingData = value;

}

if (!postTask) {

return;

}

//利用主线程Handler发送

ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable);

}

4.2 postValue 其实就是 setValue


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


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);

}

}

4.4 DefaultTaskExecutor 类


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);

}

}

4.5 setValue 方法


@MainThread

protected void setValue(T value) {

assertMainThread(“setValue”);

mVersion++;

mData = value;

dispatchingValue(null);

}

4.6 dispatchingValue 方法


@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;

}

4.7 considerNotify 判断发送数据

-------------------------------------------------------

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