does anyone have a good article explaining how to ...
# android
i
does anyone have a good article explaining how to use retrofit with coroutines and MVVM architecture
A lot of articles just launches the coroutine from activity, my goal is to launch from view model and return a value to view
n
what we use is an activity-viewmodel-usecase-repository structure. Briefly, the viewmodel extends
CoroutineScope
so it can use
launch
scopes to run coroutines methods that are defined in the use case as suspend functions. The suspend functions then use
return withContext(<http://Dispatchers.IO|Dispatchers.IO>) { // your api call }
to actually perform retrofit calls. The ViewModel then uses the returned value from the use case as a livedata that the activity observes
i
@nicopasso do i need to use JakeWharton/retrofit2-kotlin-coroutines-adapter for making api calls from within withContext
n
@Ive Vasiljevic we're not using it for now but yea I'd say that it's defintely something you wanna use