稍微深入扯下Jetpack的ViewModel

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容