Hello , someone is using <https://github.com/rickclephas/KMM-ViewModel> ? I try to put my API call i...
m
Hello , someone is using https://github.com/rickclephas/KMM-ViewModel ? I try to put my API call in the Dispatchers IO but i found nothing about it
r
Hi! Within the
KMMViewModel
class you can access the
CoroutineScope
via
viewModelScope.coroutineScope
. With that you can use coroutines like you would in any other place. Hope that helps.
m
Thank you @Rick Clephas 🙂 but it didn't solved my problem i continue my search ^^
r
Alright. Could you possibly share some of your code? It might make it a little more clear what you are trying to do.
s
@Maxime Vince i think this is little better option https://github.com/icerockdev/moko-mvvm
m
i try this:
Copy code
private fun getVlilleStations() {
    _vLilleStations.update { UiState.loading() }
    viewModelScope.coroutineScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
        val stations = _vLilleRepository.getVLilleStations()
        val location = _locationHandler.getCurrentLocation()
        val orderedList = stations
            .map { it.fields }
            .sortedBy {
                calculationByDistance(
                    location.latitude,
                    location.longitude,
                    it.localisation[0],
                    it.localisation[1]
                )
            }
        _vLilleStations.update {
            UiState.success(MapScreenData(orderedList, location))
        }
    }
}
But my logcat said to me that some frames are skipped 😞
r
Hmm alright I am not sure why those frames are skipped, but the code looks good.