"Get it" } globalScope.text = deferred.await() Toast.makeText(applicationContext, "GlobalScope", Toast.LENGTH_SHORT).show() } }
在 launchFromGlobalScope() 方法中,我直接通过 GlobalScope.launch() 启动一个协程,delay(3000) 模拟网络请求,三秒后,会弹出一个 Toast 提示。使用上是没有任何问题的,可以正常的弹出 Toast 。但是当你执行这个方法之后,立即按返回键返回上一页面,仍然会弹出 Toast 。如果是实际开发中通过网络请求更新页面的话,当用户已经不在这个页面了,就根本没有必要再去请求了,只会浪费资源。GlobalScope 显然并不符合这一特性。Kotlin 文档 中其实也详细说明了,如下所示:
Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely. Another use of the global scope is operators running in Dispatchers.Unconfined, which don’t have any job associated with them.
Application code usually should use an application-defined CoroutineScope. Using async or launch on the instance of GlobalScope is highly discouraged.
大致意思是,Global scope 通常用于启动顶级协程,这些协程在整个应用程序生命周期内运行,不会被过早地被取消。程序代码通常应该使用自定义的协程作用域。直接使用 GlobalScope 的 async 或者 launch 方法是强烈不建议的。
GlobalScope 创建的协程没有父协程,GlobalScope 通常也不与任何生命周期组件绑定。除非手动管理,否则很难满足我们实际开发中的需求。所以,GlobalScope 能不用就尽量不用。
MainScope
官方文档中提到要使用自定义的协程作用域,当然,Kotlin 已经给我们提供了合适的协程作用域 MainScope 。看一下 MainScope 的定义:
public fun MainScope(): CoroutineScope = ContextScope(SupervisorJob() + Dispatchers.Main)
记着这个定义,在后面 ViewModel 的协程使用中也会借鉴这种写法。
给我们的 Activity 实现自己的协程作用域:
class BasicCorotineActivity : AppCompatActivity(), CoroutineScope by MainScope() {}
通过扩展函数 launch() 可以直接在主线程中启动协程,示例代码如下:
private fun launchFromMainScope() { launch { val deferred = async(Dispatchers.IO) { // network request delay(3000) "Get it" } mainScope.text = deferred.await() Toast.makeText(applicationContext, "MainScope", Toast.LENGTH_SHORT).show() } }
最后别忘了在 onDestroy() 中取消协程,通过扩展函数 cancel() 来实现:
override fun onDestroy() { super.onDestroy() cancel() }
现在来测试一下 launchFromMainScope() 方法吧!你会发现这完全符合你的需求。实际开发中可以把 MainScope 整合到 BaseActivity 中,就不需要重复书写模板代码了。
ViewModelScope
如果你使用了 MVVM 架构,根本就不会在 Activity 上书写任何逻辑代码,更别说启动协程了。这个时候大部分工作就要交给 ViewModel 了。那么如何在 ViewModel 中定义协程作用域呢?还记得上面 MainScope() 的定义吗?没错,搬过来直接使用就可以了。
class ViewModelOne : ViewModel() {
private val viewModelJob = SupervisorJob() private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
val mMessage: MutableLiveData = MutableLiveData()
fun getMessage(message: String) { uiScope.launch { val deferred = async(Dispatchers.IO) { delay(2000) "post $message" } mMessage.value = deferred.await() } }
override fun onCleared() { super.onCleared() viewModelJob.cancel() } }
这里的 uiScope 其实就等同于 MainScope。调用 getMessage() 方法和之前的 launchFromMainScope() 效果也是一样的,记得在 ViewModel 的 onCleared() 回调里取消协程。
你可以定义一个 BaseViewModel 来处理这些逻辑,避免重复书写模板代码。然而 Kotlin 就是要让你做同样的事,写更少的代码,于是 viewmodel-ktx 来了。看到 ktx ,你就应该明白它是来简化你的代码的。引入如下依赖:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha03"
然后,什么都不需要做,直接使用协程作用域 viewModelScope 就可以了。viewModelScope 是 ViewModel 的一个扩展属性,定义如下:
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)) }
看下代码你就应该明白了,还是熟悉的那一套。当 ViewModel.onCleared() 被调用的时候,viewModelScope 会自动取消作用域内的所有协程。使用示例如下:
fun getMessageByViewModel() { viewModelScope.launch { val deferred = async(Dispatchers.IO) { getMessage("ViewModel Ktx") } mMessage.value = deferred.await() } }
写到这里,viewModelScope 是能满足需求的最简写法了。实际上,写完全篇,viewModelScope 仍然是我认为的最好的选择。
LiveData
Kotlin 同样为 LiveData 赋予了直接使用协程的能力。添加如下依赖:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha03"
结尾
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,在这里免费分享给大家,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~