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

Not sure why you don't need withContext(Dispatchers.IO){ } when invoke UseCase method #9

Closed
trietbui85 opened this issue Aug 22, 2019 · 2 comments

Comments

@trietbui85
Copy link

I see in WeatherViewModel, here is the method to show weather:

fun showWeather() = viewModelScope.launch {
	showLoading()
	val weatherResult = weatherUseCase()
	val tempUnit = getTemperatureUnit()
        ...

Since weatherUseCase() is a method to get weather which should be run in network thread, I wonder why don't you wrap it inside withContext(Dispatchers.IO) { }, like this:

fun showWeather() = viewModelScope.launch {
	showLoading()
	val weatherResult = withContext(Dispatchers.IO) {  weatherUseCase() }
	val tempUnit = getTemperatureUnit()
        ...
@akoufa
Copy link
Owner

akoufa commented Aug 23, 2019

Hey, I don't wrap weatherUseCase() inside withContext(Dispatchers.IO) { } . You can see here that:

Behind the scenes this behaves as if defined as fun user(...): Call and then invoked with
Call.enqueue

If you look at the Call interface and the enqueue method this always did run asynchronously and not on the main thread.

@akoufa
Copy link
Owner

akoufa commented Aug 29, 2019

I will close it, feel free to open it again if you have more questions

@akoufa akoufa closed this as completed Aug 29, 2019
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

2 participants