-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
viewModelScope.launch 默认在主线程执行,block耗时操作有问题吧 #3
Comments
block执行的方法都是suspend 修饰的,耗时操作会挂起,应该没有问题吧 |
为什么没用withContext(Dispatchers.IO)? |
这点我也有点不明白,不过retrofit的网络请求都是有自己的线程池的,不明白它们请求结果是怎么又切换回主线程的 |
这是个 viewModelScope , 看看 viewModelScope 代码大概能懂 |
Retrofit自己切了线程 |
这是retrofit默认开启了异步请求??? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/**
* 创建并执行协程
* @param block 协程中执行
* @param error 错误时执行
* @return Job
*/
protected fun launch(block: Block, error: Error? = null, cancel: Cancel? = null): Job {
return viewModelScope.launch {
try {
block.invoke()
} catch (e: Exception) {
when (e) {
is CancellationException -> {
cancel?.invoke(e)
}
else -> {
onError(e)
error?.invoke(e)
}
}
}
}
}
The text was updated successfully, but these errors were encountered: