LiveData和ViewModel源码学习

简介: LiveData和ViewModel源码学习
LiveData
ObserverWrapper 数据观察者
LifecycleEventObserver 生命周期观察者
  1. 活跃状态接受并更新数据 刷新页面数据
  2. 非活跃状态暂停接收数据 防止崩溃,
  3. 销毁状态的时候移除观察者 防止内存泄漏
  1. 数据可以监听
  1. 数据倒灌,就是先发送数据,通过生命周期响应来触发change
  1. 数据粘性,先发送数据,后订阅的也能收到数据
  1. 为什么能感知生命周期和数据观察者 因为把传入的观察者通过LifecicleBoundObserver包了一层

     LifecicleBoundObserver继承了LifecicleEventObserver和ObserverWrapper,然后添加到lifecicle和livedata中



package androidx.lifecycle;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.arch.core.internal.SafeIterableMap;
import androidx.lifecycle.Lifecycle.State;
import java.util.Iterator;
import java.util.Map;

public abstract class LiveData<T> {
    final Object mDataLock = new Object();
    static final int START_VERSION = -1;
    static final Object NOT_SET = new Object();
    private SafeIterableMap<Observer<? super T>, LiveData<T>.ObserverWrapper> mObservers = new SafeIterableMap();
    int mActiveCount = 0;
    private volatile Object mData;
    volatile Object mPendingData;
    private int mVersion;
    private boolean mDispatchingValue;
    private boolean mDispatchInvalidated;
    private final Runnable mPostValueRunnable;

    public LiveData(T value) {
        this.mPendingData = NOT_SET;
        this.mPostValueRunnable = new NamelessClass_1();
        this.mData = value;
        this.mVersion = 0;
    }

    public LiveData() {
        this.mPendingData = NOT_SET;

        class NamelessClass_1 implements Runnable {
            NamelessClass_1() {
            }

            public void run() {
                Object newValue;
                synchronized(LiveData.this.mDataLock) {
                    newValue = LiveData.this.mPendingData;
                    LiveData.this.mPendingData = LiveData.NOT_SET;
                }

                LiveData.this.setValue(newValue);
            }
        }

        this.mPostValueRunnable = new NamelessClass_1();
        this.mData = NOT_SET;
        this.mVersion = -1;
    }

    private void considerNotify(LiveData<T>.ObserverWrapper observer) {
        if (observer.mActive) {
            if (!observer.shouldBeActive()) {
                observer.activeStateChanged(false);
            } else if (observer.mLastVersion < this.mVersion) {
                observer.mLastVersion = this.mVersion;
                observer.mObserver.onChanged(this.mData);
            }
        }
    }

    void dispatchingValue(@Nullable LiveData<T>.ObserverWrapper initiator) {
        if (this.mDispatchingValue) {
            this.mDispatchInvalidated = true;
        } else {
            this.mDispatchingValue = true;

            do {
                this.mDispatchInvalidated = false;
                if (initiator != null) {
                    this.considerNotify(initiator);
                    initiator = null;
                } else {
                    Iterator<Map.Entry<Observer<? super T>, LiveData<T>.ObserverWrapper>> iterator = this.mObservers.iteratorWithAdditions();

                    while(iterator.hasNext()) {
                        this.considerNotify((ObserverWrapper)((Map.Entry)iterator.next()).getValue());
                        if (this.mDispatchInvalidated) {
                            break;
                        }
                    }
                }
            } while(this.mDispatchInvalidated);

            this.mDispatchingValue = false;
        }
    }

    @MainThread
    public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
        assertMainThread("observe");
        if (owner.getLifecycle().getCurrentState() != State.DESTROYED) {
            LiveData<T>.LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
            LiveData<T>.ObserverWrapper existing = (ObserverWrapper)this.mObservers.putIfAbsent(observer, wrapper);
            if (existing != null && !existing.isAttachedTo(owner)) {
                throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
            } else if (existing == null) {
                owner.getLifecycle().addObserver(wrapper);
            }
        }
    }

    @MainThread
    public void observeForever(@NonNull Observer<? super T> observer) {
        assertMainThread("observeForever");
        LiveData<T>.AlwaysActiveObserver wrapper = new AlwaysActiveObserver(observer);
        LiveData<T>.ObserverWrapper existing = (ObserverWrapper)this.mObservers.putIfAbsent(observer, wrapper);
        if (existing instanceof LifecycleBoundObserver) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        } else if (existing == null) {
            wrapper.activeStateChanged(true);
        }
    }

    @MainThread
    public void removeObserver(@NonNull Observer<? super T> observer) {
        assertMainThread("removeObserver");
        LiveData<T>.ObserverWrapper removed = (ObserverWrapper)this.mObservers.remove(observer);
        if (removed != null) {
            removed.detachObserver();
            removed.activeStateChanged(false);
        }
    }

    @MainThread
    public void removeObservers(@NonNull LifecycleOwner owner) {
        assertMainThread("removeObservers");
        Iterator var2 = this.mObservers.iterator();

        while(var2.hasNext()) {
            Map.Entry<Observer<? super T>, LiveData<T>.ObserverWrapper> entry = (Map.Entry)var2.next();
            if (((ObserverWrapper)entry.getValue()).isAttachedTo(owner)) {
                this.removeObserver((Observer)entry.getKey());
            }
        }

    }

    protected void postValue(T value) {
        boolean postTask;
        synchronized(this.mDataLock) {
            postTask = this.mPendingData == NOT_SET;
            this.mPendingData = value;
        }

        if (postTask) {
            ArchTaskExecutor.getInstance().postToMainThread(this.mPostValueRunnable);
        }
    }

    @MainThread
    protected void setValue(T value) {
        assertMainThread("setValue");
        ++this.mVersion;
        this.mData = value;
        this.dispatchingValue((ObserverWrapper)null);
    }

    @Nullable
    public T getValue() {
        Object data = this.mData;
        return data != NOT_SET ? data : null;
    }

    int getVersion() {
        return this.mVersion;
    }

    protected void onActive() {
    }

    protected void onInactive() {
    }

    public boolean hasObservers() {
        return this.mObservers.size() > 0;
    }

    public boolean hasActiveObservers() {
        return this.mActiveCount > 0;
    }

    static void assertMainThread(String methodName) {
        if (!ArchTaskExecutor.getInstance().isMainThread()) {
            throw new IllegalStateException("Cannot invoke " + methodName + " on a background thread");
        }
    }

    private class AlwaysActiveObserver extends LiveData<T>.ObserverWrapper {
        AlwaysActiveObserver(Observer<? super T> observer) {
            super(observer);
        }

        boolean shouldBeActive() {
            return true;
        }
    }

    private abstract class ObserverWrapper {
        final Observer<? super T> mObserver;
        boolean mActive;
        int mLastVersion = -1;

        ObserverWrapper(Observer<? super T> observer) {
            this.mObserver = observer;
        }

        abstract boolean shouldBeActive();

        boolean isAttachedTo(LifecycleOwner owner) {
            return false;
        }

        void detachObserver() {
        }

        void activeStateChanged(boolean newActive) {
            if (newActive != this.mActive) {
                this.mActive = newActive;
                boolean wasInactive = LiveData.this.mActiveCount == 0;
                LiveData var10000 = LiveData.this;
                var10000.mActiveCount += this.mActive ? 1 : -1;
                if (wasInactive && this.mActive) {
                    LiveData.this.onActive();
                }

                if (LiveData.this.mActiveCount == 0 && !this.mActive) {
                    LiveData.this.onInactive();
                }

                if (this.mActive) {
                    LiveData.this.dispatchingValue(this);
                }

            }
        }
    }

    class LifecycleBoundObserver extends LiveData<T>.ObserverWrapper implements LifecycleEventObserver {
        @NonNull
        final LifecycleOwner mOwner;

        LifecycleBoundObserver(@NonNull LifecycleOwner owner, Observer<? super T> observer) {
            super(observer);
            this.mOwner = owner;
        }

        boolean shouldBeActive() {
            return this.mOwner.getLifecycle().getCurrentState().isAtLeast(State.STARTED);
        }

        public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
            if (this.mOwner.getLifecycle().getCurrentState() == State.DESTROYED) {
                LiveData.this.removeObserver(this.mObserver);
            } else {
                this.activeStateChanged(this.shouldBeActive());
            }
        }

        boolean isAttachedTo(LifecycleOwner owner) {
            return this.mOwner == owner;
        }

        void detachObserver() {
            this.mOwner.getLifecycle().removeObserver(this);
        }
    }
}

ViewModel

1.为什么ViewModel生命周期比activity长

attach(Acitivity)->mLastNonConfigurationInstances(ActivityClientRecord)-> 恢复

performDestoryActivity(ActivityThread)-> retainNonConfigurationInstances(Activity)- onRetainConfigurationInstances(ComponentActivity)->mLastNonConfigurationInstances 保存

ViewModelStore
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package androidx.lifecycle;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class ViewModelStore {
    private final HashMap<String, ViewModel> mMap = new HashMap();

    public ViewModelStore() {
    }

    final void put(String key, ViewModel viewModel) {
        ViewModel oldViewModel = (ViewModel)this.mMap.put(key, viewModel);
        if (oldViewModel != null) {
            oldViewModel.onCleared();
        }

    }

    final ViewModel get(String key) {
        return (ViewModel)this.mMap.get(key);
    }

    Set<String> keys() {
        return new HashSet(this.mMap.keySet());
    }

    public final void clear() {
        Iterator var1 = this.mMap.values().iterator();

        while(var1.hasNext()) {
            ViewModel vm = (ViewModel)var1.next();
            vm.clear();
        }

        this.mMap.clear();
    }
}

ViewModelProvider


package androidx.lifecycle;

import android.app.Application;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import java.lang.reflect.InvocationTargetException;

public class ViewModelProvider {
    private static final String DEFAULT_KEY = "androidx.lifecycle.ViewModelProvider.DefaultKey";
    private final Factory mFactory;
    private final ViewModelStore mViewModelStore;

    public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
        this((ViewModelStore)owner.getViewModelStore(), (Factory)(owner instanceof HasDefaultViewModelProviderFactory ? ((HasDefaultViewModelProviderFactory)owner).getDefaultViewModelProviderFactory() : ViewModelProvider.NewInstanceFactory.getInstance()));
    }

    public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) {
        this(owner.getViewModelStore(), factory);
    }

    public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
        this.mFactory = factory;
        this.mViewModelStore = store;
    }

    @NonNull
    @MainThread
    public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
        String canonicalName = modelClass.getCanonicalName();
        if (canonicalName == null) {
            throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
        } else {
            return this.get("androidx.lifecycle.ViewModelProvider.DefaultKey:" + canonicalName, modelClass);
        }
    }

    @NonNull
    @MainThread
    public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) {
        ViewModel viewModel = this.mViewModelStore.get(key);
        if (modelClass.isInstance(viewModel)) {
            if (this.mFactory instanceof OnRequeryFactory) {
                ((OnRequeryFactory)this.mFactory).onRequery(viewModel);
            }

            return viewModel;
        } else {
            if (viewModel != null) {
            }

            if (this.mFactory instanceof KeyedFactory) {
                viewModel = ((KeyedFactory)((KeyedFactory)this.mFactory)).create(key, modelClass);
            } else {
                viewModel = this.mFactory.create(modelClass);
            }

            this.mViewModelStore.put(key, viewModel);
            return viewModel;
        }
    }

    public static class AndroidViewModelFactory extends NewInstanceFactory {
        private static AndroidViewModelFactory sInstance;
        private Application mApplication;

        @NonNull
        public static AndroidViewModelFactory getInstance(@NonNull Application application) {
            if (sInstance == null) {
                sInstance = new AndroidViewModelFactory(application);
            }

            return sInstance;
        }

        public AndroidViewModelFactory(@NonNull Application application) {
            this.mApplication = application;
        }

        @NonNull
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            if (AndroidViewModel.class.isAssignableFrom(modelClass)) {
                try {
                    return (ViewModel)modelClass.getConstructor(Application.class).newInstance(this.mApplication);
                } catch (NoSuchMethodException var3) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, var3);
                } catch (IllegalAccessException var4) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, var4);
                } catch (InstantiationException var5) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, var5);
                } catch (InvocationTargetException var6) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, var6);
                }
            } else {
                return super.create(modelClass);
            }
        }
    }

    public static class NewInstanceFactory implements Factory {
        private static NewInstanceFactory sInstance;

        public NewInstanceFactory() {
        }

        @NonNull
        static NewInstanceFactory getInstance() {
            if (sInstance == null) {
                sInstance = new NewInstanceFactory();
            }

            return sInstance;
        }

        @NonNull
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            try {
                return (ViewModel)modelClass.newInstance();
            } catch (InstantiationException var3) {
                throw new RuntimeException("Cannot create an instance of " + modelClass, var3);
            } catch (IllegalAccessException var4) {
                throw new RuntimeException("Cannot create an instance of " + modelClass, var4);
            }
        }
    }

    abstract static class KeyedFactory extends OnRequeryFactory implements Factory {
        KeyedFactory() {
        }

        @NonNull
        public abstract <T extends ViewModel> T create(@NonNull String var1, @NonNull Class<T> var2);

        @NonNull
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            throw new UnsupportedOperationException("create(String, Class<?>) must be called on implementaions of KeyedFactory");
        }
    }

    static class OnRequeryFactory {
        OnRequeryFactory() {
        }

        void onRequery(@NonNull ViewModel viewModel) {
        }
    }

    public interface Factory {
        @NonNull
        <T extends ViewModel> T create(@NonNull Class<T> var1);
    }
}

ViewModelStoreOwner
public interface ViewModelStoreOwner {
    @NonNull
    ViewModelStore getViewModelStore();
}

ComponentActivity
NonConfigurationInstances
  @Nullable
    public final Object onRetainNonConfigurationInstance() {
        Object custom = this.onRetainCustomNonConfigurationInstance();
        ViewModelStore viewModelStore = this.mViewModelStore;
        NonConfigurationInstances nci;
        if (viewModelStore == null) {
            nci = (NonConfigurationInstances)this.getLastNonConfigurationInstance();
            if (nci != null) {
                viewModelStore = nci.viewModelStore;
            }
        }

        if (viewModelStore == null && custom == null) {
            return null;
        } else {
            nci = new NonConfigurationInstances();
            nci.custom = custom;
            nci.viewModelStore = viewModelStore;
            return nci;
        }
    }
     @NonNull
    public ViewModelStore getViewModelStore() {
        if (this.getApplication() == null) {
            throw new IllegalStateException("Your activity is not yet attached to the Application instance. You can't request ViewModel before onCreate call.");
        } else {
            if (this.mViewModelStore == null) {
                NonConfigurationInstances nc = (NonConfigurationInstances)this.getLastNonConfigurationInstance();
                if (nc != null) {
                    this.mViewModelStore = nc.viewModelStore;
                }

                if (this.mViewModelStore == null) {
                    this.mViewModelStore = new ViewModelStore();
                }
            }

            return this.mViewModelStore;
        }
    }
     @NonNull
    public ViewModelProvider.Factory getDefaultViewModelProviderFactory() {
        if (this.getApplication() == null) {
            throw new IllegalStateException("");
        } else {
            if (this.mDefaultFactory == null) {
                this.mDefaultFactory = new SavedStateViewModelFactory(this.getApplication(), this, this.getIntent() != null ? this.getIntent().getExtras() : null);
            }

            return this.mDefaultFactory;
        }
    }
   static final class NonConfigurationInstances {
        Object custom;
        ViewModelStore viewModelStore;

        NonConfigurationInstances() {
        }
    }
Activity attach 恢复
NonConfigurationInstances
  
 @UnsupportedAppUsage
    final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor,
            Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
              mLastNonConfigurationInstances = lastNonConfigurationInstances;
            }

 NonConfigurationInstances retainNonConfigurationInstances() {
        Object activity = onRetainNonConfigurationInstance();
        NonConfigurationInstances nci = new NonConfigurationInstances();
        nci.activity = activity;
        nci.children = children;
        nci.fragments = fragments;
        nci.loaders = loaders;
        if (mVoiceInteractor != null) {
            mVoiceInteractor.retainInstance();
            nci.voiceInteractor = mVoiceInteractor;
        }
        return nci;
    }
    static final class NonConfigurationInstances {
        Object activity;
        HashMap<String, Object> children;
        FragmentManagerNonConfig fragments;
        ArrayMap<String, LoaderManager> loaders;
        VoiceInteractor voiceInteractor;
    }

ActivityThread performDestroyActivity 保存
   @UnsupportedAppUsage
    final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
  
   ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
            int configChanges, boolean getNonConfigInstance, String reason) {
        ActivityClientRecord r = mActivities.get(token);
        Class<? extends Activity> activityClass = null;
        if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
        if (r != null) {
            activityClass = r.activity.getClass();
            r.activity.mConfigChangeFlags |= configChanges;
            if (finishing) {
                r.activity.mFinished = true;
            }
            performPauseActivityIfNeeded(r, "destroy");
            if (getNonConfigInstance) {
                try {
                    r.lastNonConfigurationInstances
                            = r.activity.retainNonConfigurationInstances();
                } catch (Exception e) {
                    if (!mInstrumentation.onException(r.activity, e)) {
                        throw new RuntimeException(
                                "Unable to retain activity "
                                + r.intent.getComponent().toShortString()
                                + ": " + e.toString(), e);
                    }
                }
            }
         
        return r;
    }


ActivityClientRecord
  public static final class ActivityClientRecord {
        @UnsupportedAppUsage
        public IBinder token;
        Activity.NonConfigurationInstances lastNonConfigurationInstances;
        
 }
相关文章
|
5月前
|
存储 移动开发 数据库
构建高效Android应用:探究LiveData和ViewModel的最佳实践
【4月更文挑战第20天】 在动态演化的移动开发领域,构建一个既响应迅速又能够在用户界面保持稳定的Android应用是至关重要的。近年来,随着Android架构组件的推出,特别是LiveData和ViewModel的引入,开发者得以更有效地管理应用状态并优化用户界面的响应性。本文将深入探讨LiveData和ViewModel的实现机制,并通过案例分析展示如何结合它们来构建一个高效且健壮的Android应用架构。我们将重点讨论如何通过这些组件简化数据绑定过程、提高代码的可维护性和测试性,同时确保用户界面的流畅性。
|
5月前
|
安全
一道面试题:介绍一下 LiveData 的 postValue ?
一道面试题:介绍一下 LiveData 的 postValue ?
70 0
|
5月前
|
存储 前端开发
CreationExtras 来了,创建 ViewModel 的新方式
CreationExtras 来了,创建 ViewModel 的新方式
92 0
|
3月前
|
存储 前端开发 测试技术
Android Kotlin中使用 LiveData、ViewModel快速实现MVVM模式
使用Kotlin实现MVVM模式是Android开发的现代实践。该模式分离UI和业务逻辑,借助LiveData、ViewModel和DataBinding增强代码可维护性。步骤包括创建Model层处理数据,ViewModel层作为数据桥梁,以及View层展示UI。添加相关依赖后,Model类存储数据,ViewModel类通过LiveData管理变化,而View层使用DataBinding实时更新UI。这种架构提升代码可测试性和模块化。
145 2
|
5月前
|
前端开发
MVVM LiveData+DataBinding+Lifecycle+ViewModel架构
MVVM LiveData+DataBinding+Lifecycle+ViewModel架构
54 1
|
Android开发
Dagger Hilt - ViewModel的依赖注入及实现原理
Dagger Hilt VIewModel 依赖注入的原理
461 0
|
Android开发 容器
Android ViewModel与LiveData组件组合使用详解
Android ViewModel与LiveData组件组合使用详解
120 0
|
存储 缓存 前端开发
学会使用LiveData和ViewModel,我相信会让你在写业务时变得轻松🌞
当你学会如何使用LiveData和ViewModel后,你会发现,世界变得简单了... 在2017年,那时,观察者模式有效的简化了开发,但是诸如RxJava一类的库有一些太过复杂,学习成本
|
Android开发
Livedata用的爽吗,StateFlow你也应该了解一下
Livedata用的爽吗,StateFlow你也应该了解一下
741 0
Livedata用的爽吗,StateFlow你也应该了解一下
|
开发工具 Android开发 git
android Livedata最详尽的使用场景分析,让你爱上Livedata
android Livedata最详尽的使用场景分析,让你爱上Livedata
986 0
android Livedata最详尽的使用场景分析,让你爱上Livedata