Issue
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.
So when printing the current Thread in viewModelScope.launch it gives Thread[main,5,main]
But here is my question. Although this runs in Main Thread, the following code works for me which performs a network call.
viewModelScope.launch {
userRepo.login(email, password)
}
Here userRepo.login(email, password) is suspend function, which calls Retrofit suspend function.
So how this works, if my Current Thread is Main Thread?
Solution
It works because Retrofit's suspend implementation delegates to Call<T>.enqueue. This means it already executes on its own background executor by default instead of using the caller's Dispatcher.
Answered By - Kiskae
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.