Skip to content
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

Open
sunqi94600 opened this issue May 20, 2020 · 6 comments

Comments

@sunqi94600
Copy link

/**
* 创建并执行协程
* @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)
}
}
}
}
}

@zenghuiwang
Copy link

block执行的方法都是suspend 修饰的,耗时操作会挂起,应该没有问题吧

@andev009
Copy link

为什么没用withContext(Dispatchers.IO)?

@yu893145181
Copy link

这点我也有点不明白,不过retrofit的网络请求都是有自己的线程池的,不明白它们请求结果是怎么又切换回主线程的

@Crazy-MT
Copy link

这是个 viewModelScope , 看看 viewModelScope 代码大概能懂

@jiayuanfa
Copy link

Retrofit自己切了线程

@xingxing16
Copy link

这是retrofit默认开启了异步请求???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants