前提
在Android MVVM模式,我使用了Jetpack包中的ViewModel来实现业务层,当然你也可以使用DataBinding。
业务层无非就是网络请求,存储操作和数据处理操作,然后将处理好的数据更新给LiveData,UI层则自动更新。其中网络请求我是使用的协程来进行,而不是线程。
问题
为了防止UI销毁时异步任务仍然在进行所导致的内存泄露,我们都会在onCleared()方法中去取消异步任务。如何取消异步任务呢?懒惰的我们当然不会在每个ViewModel中去取消,而是去定义一个BaseVM类来存储每个Job对象,然后统一取消。代码如下:
open class BaseVM : ViewModel(){
val jobs = mutableListOf<Job>()
override fun onCleared() {
super.onCleared()
jobs.forEach { it.cancel() }
}
}
//UserVM
class UserVM : BaseVM() {
val userData = StateLiveData<UserBean>()
fun login() {
jobs.add(GlobalScope.launch {
userData.postLoading()
val result = "https://lixiaojun.xin/api/login".http(this).get<HttpResult<UserBean>>().await()
if (result != null && result.succeed) {
userData.postValueAndSuccess(result.data!!)
} else {
userData.postError()
}
})
}
fun register(){
//...
}
}
这样写看起来简洁统一,但并不是最优雅的,它有两个问题:
- 需要我们手动取消,现在是9102年,不该啊
- 不够灵活,它会傻瓜式的取消所有VM的异步任务,如果我们某个VM的某个异步任务的需求是即使