https://kotlinlang.org logo
Title
i

Ive Vasiljevic

03/04/2019, 12:54 PM
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

nicopasso

03/04/2019, 1:35 PM
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

Ive Vasiljevic

03/05/2019, 8:57 AM
@nicopasso do i need to use JakeWharton/retrofit2-kotlin-coroutines-adapter for making api calls from within withContext
n

nicopasso

03/05/2019, 9:21 AM
@Ive Vasiljevic we're not using it for now but yea I'd say that it's defintely something you wanna use