关于协程,详解ViewModelScope

ViewModelScope虽然是协程,但属于androidx.lifecycle包中ViewModel的扩展属性

示例:

class MyViewModel :ViewModel() {

fun getData(){

viewModelScope.launch {

// do

}

}

}

使用非常简单,关键在于它是怎么保证不会内存泄露的?

源码分析


来看viewModelScope源码:

public val ViewModel.viewModelScope: CoroutineScope

get() {

val scope: CoroutineScope? = this.getTag(JOB_KEY)

if (scope != null) {

return scope

}

return setTagIfAbsent(

JOB_KEY,

CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)

)

}

internal class CloseableCoroutineScope(context: CoroutineContext) : Closeable, CoroutineScope {

override val coroutineContext: CoroutineContext = context

override fun close() {

coroutineContext.cancel()

}

}

先看get()方法:

get() {

val scope: CoroutineScope? = this.getTag(JOB_KEY)

if (scope != null) {

return scope

}

return setTagIfAbsent(

JOB_KEY,

CloseableCoroutineScope(SupervisorJob() +

Dispatchers.Main.immediate)

)

}

return中通过setTagIfAbsent创建了协程,并且指定主线程

先忽略setTagIfAbsent,来看协程创建的方式:

internal class CloseableCoroutineScope(context: CoroutineContext) : Closeable, CoroutineScope {

override val coroutineContext: CoroutineContext = context

override fun close() {

coroutineContext.cancel()

}

}

CloseableCoroutineScope,顾名思义,可以关闭的协程。

实现Closeable接口,并重写唯一方法close(),并在方法中取消了协程。

现在我们已经知道了viewModelScope是可以取消的了,关键就在于取消时机的控制了。

回过头在再看setTagIfAbsent,setTagIfAbsent是ViewModel中的方法

public abstract class ViewModel {

@Nullable

private final Map<String, Object> mBagOfTags = new HashMap<>();

private volatile boolean mCleared = false;

@SuppressWarnings(“WeakerAccess”)

protected void onCleared() {

}

@MainThread

final void clear() {

mCleared = true;

if (mBagOfTags != null) {

synchronized (mBagOfTags) {

for (Object value : mBagOfTags.values()) {

closeWithRuntimeException(value);

}

}

}

onCleared();

}

@SuppressWarnings(“unchecked”)

T setTagIfAbsent(String key, T newValue) {

T previous;

synchronized (mBagOfTags) {

previous = (T) mBagOfTags.get(key);

if (previous == null) {

mBagOfTags.put(key, newValue);

}

}

T result = previous == null ? newValue : previous;

if (mCleared) {

closeWithRuntimeException(result);

}

return result;

}

@SuppressWarnings({“TypeParameterUnusedInFormals”, “unchecked”})

T getTag(String key) {

if (mBagOfTags == null) {

return null;

}

synchronized (mBagOfTags) {

return (T) mBagOfTags.get(key);

}

}

private static void closeWithRuntimeException(Object obj) {

if (obj instanceof Closeable) {

try {

((Closeable) obj).close();

} catch (IOException e) {

throw new RuntimeException(e);

}

}

}

}

在setTagIfAbsent中,以HashMap的形式把协程对象保存起来了,并配有getTag方法。

可能有同学已经注意到最后的方法closeWithRuntimeException,因为这个方法中调用了Closeable接口的close()方法,而close()方法就是用来取消协程的。

而closeWithRuntimeException方法是谁调用的呢,主要是ViewModel中的clear()方法。

@MainThread

final void clear() {

mCleared = true;

if (mBagOfTags != null) {

synchronized (mBagOfTags) {

for (Object value : mBagOfTags.values()) {

closeWithRuntimeException(value);

}

}

}

onCleared();

}

这里是循环保存协程的HashMap,然后调用closeWithRuntimeException取消协程。

那这个ViewModel中的clear()方法又是谁调用的呢?

查看源码,只有一处调用,就是在ViewModelStore

public class ViewModelStore {

private final HashMap<String, ViewModel> mMap = new HashMap<>();

final void put(String key, ViewModel viewModel) {

ViewModel oldViewModel = mMap.put(key, viewModel);

if (oldViewModel != null) {

oldViewModel.onCleared();

}

}

final ViewModel get(String key) {

return mMap.get(key);

}

Set keys() {

return new HashSet<>(mMap.keySet());

}

public final void clear() {

for (ViewModel vm : mMap.values()) {

vm.clear();

}

mMap.clear();

}

}

ViewModelStore的源码比较少,也很简单。

同样也是以HashMap的形式来保存ViewModel。

那是什么时候保存的呢,我们来追踪一下put方法:

public class ViewModelProvider {

//…

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
**

[外链图片转存中…(img-PkJV3YDw-1715883968070)]

[外链图片转存中…(img-7k7DMdTA-1715883968074)]

[外链图片转存中…(img-rj8OremI-1715883968076)]

[外链图片转存中…(img-MhAtgGfM-1715883968078)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值