美文网首页
稍微深入扯下Jetpack的ViewModel

稍微深入扯下Jetpack的ViewModel

作者: 三月四晴 | 来源:发表于2022-01-14 17:54 被阅读0次

    Jetpack出来很久了,最近看到别人又在扯ViewModel,带着几个问题再去重新看看

    问问题之前可以先简单回顾下之前写的Jetpack mvvm 三部曲(一) ViewModel

    更方便看我在代码片段添加了注释

    1. ViewModel怎么创建的?

    从这段创建的代码开始

    MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);
    

    先进来看下这段代码ViewModelProvider的相关方法

    public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
    
    //这个构造参数有个值得注意的细节就是这个三目运算符,开头传的是this是一个实体类可以看作是ComponentActivity本身所以owner instanceof HasDefaultViewModelProviderFactory这个是成立的
      this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory
                ? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory()
                : NewInstanceFactory.getInstance());
    }
    
    public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
        mFactory = factory;
        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");
        }
        return get(DEFAULT_KEY + ":" + canonicalName, modelClass);
    }
    
    @NonNull
    @MainThread
    public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) {
        ViewModel viewModel = mViewModelStore.get(key);
    
        if (modelClass.isInstance(viewModel)) {
            if (mFactory instanceof OnRequeryFactory) {
                ((OnRequeryFactory) mFactory).onRequery(viewModel);
            }
            return (T) viewModel;
        } else {
            //noinspection StatementWithEmptyBody
            if (viewModel != null) {
                // TODO: log a warning.
            }
        }
        //第一次进来会走这个因为上面mFactory是继成自KeyedFactory
        if (mFactory instanceof KeyedFactory) {
          //这里实际上就是调用SavedStateViewModelFactory#create方法
          //篇幅关系就在这里稍微扯下 这里最终就是通过反射实例化了
            viewModel = ((KeyedFactory) (mFactory)).create(key, modelClass);
        } else {
            viewModel = (mFactory).create(modelClass);
        }
        //所以这个key是可以自己定义名称的
        //这个mViewModelStore实际上就是Activtiy的mViewModelStore
        mViewModelStore.put(key, viewModel);
        return (T) viewModel;
    }
    
    image.png

    这个ComponentActivity是实现了ViewModelStoreOwner,HasDefaultViewModelProviderFactory,这俩接口

    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        if (getApplication() == null) {
            throw new IllegalStateException("Your activity is not yet attached to the "
                    + "Application instance. You can't request ViewModel before onCreate call.");
        }
        if (mViewModelStore == null) {
            //初始很定是为空的
            //这里去NonConfigurationInstances拿一下,看到这里ams知识很好的的同学估计已经知道第二个问题的答案了
            NonConfigurationInstances nc =
                    (NonConfigurationInstances) getLastNonConfigurationInstance();
            if (nc != null) {
                 //从 NonConfigurationInstances 恢复 ViewModelStore
                mViewModelStore = nc.viewModelStore;
            }
            if (mViewModelStore == null) {
            //这里不用说了
                mViewModelStore = new ViewModelStore();
            }
        }
        return mViewModelStore;
    }
    

    下面这个是HasDefaultViewModelProviderFactory的实现

    @NonNull
    @Override
    public ViewModelProvider.Factory getDefaultViewModelProviderFactory() {
        if (getApplication() == null) {
            throw new IllegalStateException("Your activity is not yet attached to the "
                    + "Application instance. You can't request ViewModel before onCreate call.");
        }
        if (mDefaultFactory == null) {
            mDefaultFactory = new SavedStateViewModelFactory(
                    getApplication(),
                    this,
                    getIntent() != null ? getIntent().getExtras() : null);
        }
        return mDefaultFactory;
    }
    //这里的SavedStateViewModelFactory是继承ViewModelProvider.KeyedFactory
    

    调用ViewModelProvider的构造方法传入了一个接口实现类this,所以在一个参数的构造方法调用传入的参数是ComponentActivity#mViewModelStore以及mDefaultFactory,到这里大致清楚了ViewModel的创建了

    2.他是在页面重新创建的时候恢复的?

    这点算ViewModel的核心吧,想要知道答案那就得还得看下ViewModelStore这个东西,上面贴了完整的ComponentActivity#getViewModelStore方法,为了方便再这贴下geViewModelStore的关键代码

    NonConfigurationInstances nc =
                    (NonConfigurationInstances) getLastNonConfigurationInstance();
            if (nc != null) {
              
                // Restore the ViewModelStore from NonConfigurationInstances
                mViewModelStore = nc.viewModelStore;
            }
    
    image.png

    关键点nc.viewModelStore探究下getLastNonConfigurationInstance是啥

    @Nullable
    public Object getLastNonConfigurationInstance() {
        return mLastNonConfigurationInstances != null
                ? mLastNonConfigurationInstances.activity : null;
    }
    

    先去找找mLastNonConfigurationInstances的赋值在找下activity的赋值

    image.png

    这里mLastNonConfigurationInstances是从attach方法赋值了,attach这个方法的调用是ActivityThread调用的(这块涉及到了ams的启动感兴趣的同学可以去搜索相关文章)

    image.png

    这里找到mLastNonConfigurationInstances的赋值了找下activity的赋值

    NonConfigurationInstances retainNonConfigurationInstances() {
         //这个onRetainNonConfigurationInstance是值得关注的
        Object activity = onRetainNonConfigurationInstance();
        HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
        FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
    
        // We're already stopped but we've been asked to retain.
        // Our fragments are taken care of but we need to mark the loaders for retention.
        // In order to do this correctly we need to restart the loaders first before
        // handing them off to the next activity.
        mFragments.doLoaderStart();
        mFragments.doLoaderStop(true);
        ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
    
        if (activity == null && children == null && fragments == null && loaders == null
                && mVoiceInteractor == null) {
            return null;
        }
    
        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;
    }
    

    上面onRetainNonConfigurationInstance方法在Activity是return null肯定是有地方重写了的;所以在
    ComponentActivity找到了这个方法的重写

    //这里贴下NonConfigurationInstances这个是ComponentActivity的内部静态类
    static final class NonConfigurationInstances {
        Object custom;
        ViewModelStore viewModelStore;
    }
    
    /**
     * Retain all appropriate non-config state.  You can NOT
     * override this yourself!  Use a {@link androidx.lifecycle.ViewModel} if you want to
     * retain your own non config state.
     */
    @Override
    @Nullable
    public final Object onRetainNonConfigurationInstance() {
         
        Object custom = onRetainCustomNonConfigurationInstance();
    
        ViewModelStore viewModelStore = mViewModelStore;
        if (viewModelStore == null) {
           //没有人调用 getViewModelStore(),
           //所以看看我们上一个 NonConfigurationInstanc 中是否有一个现有的ViewModelStore
            NonConfigurationInstances nc =
                    (NonConfigurationInstances) getLastNonConfigurationInstance();
            if (nc != null) {
                viewModelStore = nc.viewModelStore;
            }
        }
    
        if (viewModelStore == null && custom == null) {
            return null;
        }
        
        NonConfigurationInstances nci = new NonConfigurationInstances();
        nci.custom = custom;
        //到这里不就是把viewModelStore塞进去了么
        nci.viewModelStore = viewModelStore;
        return nci;
    }
    

    这里只是保存的过程那么谁调用了Activity#retainNonConfigurationInstances方法呢?

    • 在Activity横竖屏的时候ams会走到这个ActvitiyThread#performDestroyActivity方法执行Activiy#retainNonConfigurationInstances,所以重走生命周期也不会丢失ViewModel的数据在这;重新恢复页面的时候只要通过getViewModelStore方法就可以拿到原来的数据了
    image.png
    3.又是如何销毁的?

    直接看ComponentActivity构造方法就好了监测到Lifecycle发过来的销毁信号执行清空方法

    image.png

    ViewModelStore#clear方法遍历了map去清空ViewModel并且把这个HashMap清空

    image.png

    可以在继承ViewModel的时候去重写onCleared方法做一些额外的操作比如断开上下文联系啥的

    image.png
    这里这个清空操作其实已经帮我们把这个ViewModel和Acitviy分离了关系,所以在页面销毁的时候会正常回收ViewModel不会造成啥内存泄漏;当然这个并不是绝对的;毕竟内存泄漏的根本原因就是一个生命周期长的持有了生命周期短的,导致没法回收(GC是根据根可达来分析该对象是否可以回收);所以小伙伴写代码的时候要多注意点;毕竟内存泄漏多了就会引起Out Of Memory内存溢出
    4. Fragment中ViewModel又是怎么创建的?
    • 本来想把这个和第一点一起聊的,但是为了段落分明就单独写一个吧
    MyFragmentViewModel viewModel = new ViewModelProvider(this).get(MyFragmentViewModel.class);
    

    在Fragment使用还是和Activity一样的,区别就在于Fragment中ViewModelStoreOwner,HasDefaultViewModelProviderFactory的实现
    和ComponentActivity不同;只需看ViewModelStoreOwner的实现好了

    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        if (mFragmentManager == null) {
            throw new IllegalStateException("Can't access ViewModels from detached fragment");
        }
        return mFragmentManager.getViewModelStore(this);
    }
    

    这个mNonConfig是继承ViewMolde的子类FragmentManagerViewModel看下他是如何创建的

    @NonNull
    ViewModelStore getViewModelStore(@NonNull Fragment f) {
        return mNonConfig.getViewModelStore(f);
    }
    

    可以看到FragmentManagerImpl#attachController方法有3个不同创建的这个地方就排查下

    image.png

    首先 Activity其实都有自己的FragmentController来管里Fragment;找到
    FragmentActivity;

    final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
    
    image.png

    这里在传了一个空的parent结合上面的分析;


    image.png

    创建viewModelStore是中间那个判断条件直接去看getInstance方法;

    ViewModelStore viewModelStore = ((ViewModelStoreOwner) host).getViewModelStore();
    mNonConfig = FragmentManagerViewModel.getInstance(viewModelStore);
    

    FragmentManagerViewModel#getInstance这里又去掉了ViewModelStore的get方法使用剩下的就是把这个FragmentManagerViewModel添加到Activity的Viewmodel去了;然后get实例化 MyFragmentViewModel的时候又把这个ViewModel添加到FragmentManagerViewModel去了

    @NonNull
    static FragmentManagerViewModel getInstance(ViewModelStore viewModelStore) {
        ViewModelProvider viewModelProvider = new ViewModelProvider(viewModelStore,
                FACTORY);
        return viewModelProvider.get(FragmentManagerViewModel.class);
    }
    

    正因为是这样Activtiy和Fragment可以互相拿到彼此的ViewModel

    相关文章

      网友评论

          本文标题:稍微深入扯下Jetpack的ViewModel

          本文链接:https://www.haomeiwen.com/subject/fntksltx.html