Recently I've updated my ViewModel
to use new viewModelScope
. From its' implementation, I see that Dispatchers.Main.immediate
is set as the default CoroutineDispatcher
for viewModelScope
.
所以当在viewModelScope.launch
中打印当前的Thread
时,会得到Thread[main,5,main]
但这是我的问题.虽然这是在主线程中运行的,但下面的代码适用于执行网络调用的我.
viewModelScope.launch {
userRepo.login(email, password)
}
Here userRepo.login(email, password)
is suspend
function, which calls Retrofit
suspend
function.
那么,如果我当前的线程是主线程,这是如何工作的呢?