Kotlin 协程作用域源码剖析

34 阅读3分钟

GlobalScope、MainScope、lifecycleScope、viewModelScope 四者的实现原理与正确使用姿势

1. viewModelScope

class MyViewModel : ViewModel() {
    fun loadData() {
        viewModelScope.launch {
            // 在这里启动的协程会自动绑定 ViewModel 生命周期
            // ViewModel 被清理时,协程会被取消
        }
    }
}

源码分析

// 用 ViewModel 的 tag 机制缓存 CoroutineScope,保证一个 ViewModel 实例只有一个 Scope
private const val JOB_KEY = "androidx.lifecycle.ViewModelCoroutineScope.JOB_KEY"val ViewModel.viewModelScope: CoroutineScope
    get() {
        // 1. 先从 ViewModel 的 Tag 缓存中读取
        val scope: CoroutineScope? = this.getTag(JOB_KEY)
        if (scope != null) {
            return scope
        }
        // 2. 首次访问时创建:使用 SupervisorJob,子协程失败不会相互影响
        //    Dispatchers.Main.immediate 保证 launch 块立即在当前主线程执行(若已在主线程)
        return setTagIfAbsent(JOB_KEY, CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate))
    }
​
// 包装一层 CloseableCoroutineScope 是为了让 ViewModel 在 onCleared() 时能关闭并取消 Scope
internal class CloseableCoroutineScope(context: CoroutineContext) : Closeable, CoroutineScope {
    override val coroutineContext: CoroutineContext = context
​
    override fun close() {
        // 当 ViewModel 被清除时,调用此方法取消整个 Job 树
        // 所有未完成的子协程会收到取消信号,避免内存泄漏
        coroutineContext[Job]?.cancel()
    }
}

要点: viewModelScopeViewModel 生命周期绑定;子协程之间通过 SupervisorJob 隔离失败;ViewModel.onCleared() 会触发 CloseableCoroutineScope.close() 从而取消所有协程。

2. lifecycleScope

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            // 与 Activity / Fragment 生命周期绑定
            // 在 ON_DESTROY 时自动取消协程
        }
    }
}

源码分析

// 伪代码逻辑展示核心机制
internal class LifecycleCoroutineScopeImpl(
    override val lifecycle: Lifecycle,
    override val coroutineContext: CoroutineContext
) : LifecycleCoroutineScope(), LifecycleEventObserver {
​
    init {
        // 关键:注册生命周期观察者,监听 DESTROY 事件
        lifecycle.addObserver(this)
    }
​
    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        if (event == Lifecycle.Event.ON_DESTROY) {
            // 当接收到 DESTROY 事件时,自动取消协程
            // 避免在 Activity/Fragment 销毁后继续执行 UI 更新或耗时操作
            coroutineContext[Job]?.cancel()
        }
    }
}
​
val LifecycleOwner.lifecycleScope: LifecycleCoroutineScope
    get() {
        // 每个 LifecycleOwner 只创建一次 Scope,后续复用
        return findOrCreateScope()
    }

要点: lifecycleScope 监听 Lifecycle.Event.ON_DESTROY;在页面销毁时自动取消协程,防止空指针和内存泄漏。

3. MainScope

// 适用于不依赖 Activity/Fragment/ViewModel 的顶层协程管理场景
private val scope = MainScope()
​
scope.launch {
    // 在主线程执行的协程
}
​
// MainScope 本质上就是 SupervisorJob + Dispatchers.Main
public fun MainScope(): CoroutineScope = ContextScope(SupervisorJob() + Dispatchers.Main)

要点: MainScope() 创建了一个挂在主线程的顶层作用域,需要手动调用 scope.cancel() 结束生命周期,否则可能造成泄漏。

4. GlobalScope

// 不推荐直接使用,协程生命周期跟随整个应用进程
GlobalScope.launch { }
​
@DelicateCoroutinesApi
public object GlobalScope : CoroutineScope {
    // EmptyCoroutineContext 表示没有绑定 Job 和调度器
    // 使用时会自动创建 Job,但无法从外部统一取消
    override val coroutineContext: CoroutineContext
        get() = EmptyCoroutineContext
}

要点: GlobalScope 是全局单例,没有统一取消入口;仅适合需要在整个应用生命周期内运行的后台任务,通常应避免使用,否则容易出现不可控的协程泄漏。

总结

作用域生命周期绑定是否需要手动取消推荐场景
viewModelScopeViewModel在 ViewModel 中发起网络请求、数据库操作
lifecycleScopeActivity / Fragment在 UI 层随页面创建/销毁的轻量任务
MainScope()手动管理类级别、不依赖生命周期组件的顶层协程
GlobalScope应用进程否(但无法统一取消)原则上避免,仅用于特殊全局后台任务

一句话:有生命周期就绑生命周期,没生命周期再考虑 MainScope,尽量不用 GlobalScope